Separating log and tracing

I have a few programs that need two different types of logging: production logging and detailed development logging.

The idea is that customers running the programs in production only use log (specifically env_logger). If they run into some really difficult-to-track-down problems, and we exhaust all other means of finding the issue, they are given instructions on how to enable "dev logs" (i.e. tracing, in this case).

The 'log' logging is always directed to a platform-specific output. The trace logging is meant to be able to be output to the console or to a file.

I'm going to be honest: I find the tracing crate to be quite overwhelming, and I'm pretty sure it is wildly over-competent for our modest use-case. With that said, it has been serving us well so far.

In earlier project we haven't supported outputting trace logs to a file, which is why I'm writing this post. We having been run log and tracing next to each other without any issues, but when I wrote a function to initialize tracing to a file, env_logger panics because it is called twice.

The tracing-to-file initialization looks like this:


What do I need to avoid doing with tracing/tracing-subscriber to make sure it doesn't step on the toes of env_logger? (Note that if I enable tracing output to the console rather than the file everything works fine).

What's the specific panic message?

thread 'main' panicked at 'Builder::init should not be called after logger initialized: SetLoggerError(())', C:\Users\jan\.cargo\registry\src\\env_logger-0.9.0\src\

The only change between working and not working is switching these:

//init_console_tracing();  // works
init_file_tracing(None, None);  // doesn't work

By default tracing_subscriber also captures all log messages. You can disable the default features on the crate, and manually enable all of the defaults except tracing_log, or you can just not setup the normal env_logger logging when tracing is enabled since the logs (should) show up in the tracing output.

1 Like

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.