Env_logger inside combined logger?

Can I put an env_logger to a file as one output of a combined logger?

Use case: Log messages in a GUI program go to combined_logger. ERROR and WARN messages go to to a window. Right now, INFO and above go to a file. That works. I want to make what goes to a file configurable with an environment variable, env-logger style. Is that easy to do?

Worse than I thought. env_logger can't log to a file:

Stdout

Logs will be sent to standard output.

Stderr

Logs will be sent to standard error.

Pipe(Box<dyn Write + Send + 'static>)

:-1:Deprecated:
This functionality is broken and nobody is working on fixing it
Logs will be sent to a custom pipe.

So, what's now being used for environment-configurable logging?

I've been using tracing with tracing_appender

I believe tracing now supports logging different events to different subscribers, but I haven't experimented with that so I'm not sure if it would meet your needs (or how much work it would be)

That solves a different problem.

I need selective logging. I have a GUI program with WGPU, and WGPU generates INFO level logging on every frame. I need to suppress that, or I get gigabytes of log files. I need to see INFO level logging from just what I'm currently working on.

env-logger is the right idea on filtering. It's that the output options are too limited. The developers looked at offering logging to a file, but then got stuck thinking about long-lived programs, log file rotation, and such.

Tracing supports similar filtering options to env-logger. Tracing does do more than just traditional logging, but it does traditional logging just fine too in my experience.

I already have the code set to be buildable with "tracy" on, but that's a different mechanism. Here, I just need module-level filtering for log items.

The reason env_logger won't play with combined_logger is that combined_logger tracks the highest log level needed by any component logger. So unwanted messages don't get evaluated, and that's cheap. Env_logger requires evaluating a regular expression for each item, and that's expensive if you have a lot of log level log::trace! or log::debug! calls. Env_logger is really for systems where, in production, logging is off.

Env_logger's inability to write to a pipe or file seems to come from a bikeshedding problem. There are people who really like colors in their logs. But there is disagreement over how to represent colored text in log files. ANSI terminal controls? VT100? HTML? So, due to a change in the coloring module, output to a pipe was taken out.

So, for now, I have a setup where, if the environment variable for env_logger is present, env_logger is used and output goes to standard error, which I send to a file. This is enough that I can debug. If no environment variable is present, errors go to a window and errors and warnings go to a log file, using combined_logger. It's an OK workaround for now.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.