rust-lang / log

Logging implementation for Rust
https://docs.rs/log
Apache License 2.0
2.16k stars 250 forks source link

What could cause logs not to print from libraries? #545

Closed brecht-derooms closed 1 year ago

brecht-derooms commented 1 year ago

I have built an application with quite some tracing (trace! via the log library). Later I moved a big chunk of it to a separate library in lib/ and imported it as a package to cleanly separated it.

After I was done I noticed my logs were done although in my main application, env_logger is still initialized. (I also tried another logger like sensible_env_logger but with the same result). I printed the 'static log level' in the library which prints 'TRACE'. Initially, I had put separate log levels for the different packages (which works for actix_web, where I do get logs). To be certain that nothing is ill configured I just moved to the simpler configuration:

RUST_LOG='trace' GLOBAL_RUST_LOG='trace'

Still, nothing is printed and it seems all trace!, info!, debug! or any other level are ignored. If I replace all of them with println! they do get printed. Am I missing something, do we need some kind of extra setting to use it as a separate package in lib/?

Thomasdezeeuw commented 1 year ago

@brecht-derooms what kind of logging implementation are you using? The log crate only provides a facade and doesn't differentiate between main/lib, but the logging implementation could.

brecht-derooms commented 1 year ago

I tried env_logger and sensible_env_logger as mentioned above in the initial message, which are logging implementations if I'm not mistaken. So I use 'log' everywhere and init the env_logger in my main application. To be clear, my 'sub-package' does not have a logging implementation configured, only my main package when starting up initializes a logging implementation, which is how I understood that it is supposed to work. This worked fine as long as the code wasn't in a separate 'sub-package' in lib/. It still works fine for other logs/traces/info which are not in my new sub-package, such as logs from external libraries or logs from my main application, e.g. traces from actix-web or from my 'main' package are still coming through, only each log from my 'sub-package' is ignored.

It's strange, I'm probably doing something silly that prevents it from working, I was hoping there was someone who had a clue what could be wrong. Is there something I could have accidentally done that makes it so that a sub-package's logs are suppressed? I already tried to set the global log level to trace as I wrote initially. At the moment the only thing that seems to work is to change every occurrence of trace! or info! to println! to see what is happening.

Thomasdezeeuw commented 1 year ago

I tried env_logger and sensible_env_logger as mentioned above in the initial message, which are logging implementations if I'm not mistaken. So I use 'log' everywhere and init the env_logger in my main application. To be clear, my 'sub-package' does not have a logging implementation configured, only my main package when starting up initializes a logging implementation, which is how I understood that it is supposed to work.

Yes you only have to initialise the logger once.

This worked fine as long as the code wasn't in a separate 'sub-package' in lib/. It still works fine for other logs/traces/info which are not in my new sub-package, such as logs from external libraries or logs from my main application, e.g. traces from actix-web or from my 'main' package are still coming through, only each log from my 'sub-package' is ignored.

It's strange, I'm probably doing something silly that prevents it from working, I was hoping there was someone who had a clue what could be wrong. Is there something I could have accidentally done that makes it so that a sub-package's logs are suppressed? I already tried to set the global log level to trace as I wrote initially. At the moment the only thing that seems to work is to change every occurrence of trace! or info! to println! to see what is happening.

I not to familiar with the env_logger crates, but some possible causes:

Have you tried another log implementation? Otherwise perhaps it's worth opening an issue with the env_logger crate as the log crate (this crate) doesn't differentiate based on where the log macros are called from.

brecht-derooms commented 1 year ago

I have indeed tried other log implementations as mentioned (sensible logger) with the same results. However, I am going to close this issue in order not to waste your time further. The same code now does log after working on it further (yet not changing anything to logs, just fixed a bug in cookies. Maybe something was wrong with the compilation and I should have run a 'clean' to fix it. Thanks for the help, sorry if this took a bit of time from you.