Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to use tracing with a different logger? #1237

Closed
mzhang28 opened this issue Feb 15, 2021 · 8 comments
Closed

Is it possible to use tracing with a different logger? #1237

mzhang28 opened this issue Feb 15, 2021 · 8 comments

Comments

@mzhang28
Copy link

I'm currently using fern, and it seems that I can't have both fern and tracing-subscriber since they're both in some kind of global subscriber slot. I prefer fern since it's already hooked up to my command-line options and most importantly, lets me write to a file, a feature that I've seen asked about in multiple different issues with no clear resolution. Since I'm developing an application that runs a raw-mode TUI, it's very important that all my logs go to a different file rather than stdout or stderr.

I figured that since the subscriber is global, would it be possible for me to have tracing send events to whatever the log crate usually sends to as well?

@cdesch
Copy link

cdesch commented May 29, 2022

@iptq did you ever find a solution for this issue? I'm using fern in a project as well and running into issues with tracing.

@notgull
Copy link

notgull commented May 29, 2022

To me, it looks like fern uses the log crate for its dispatcher. If you enable the log feature in tracing, it should be able to emit logging events to fern if a subscriber isn't implemented.

@cdesch
Copy link

cdesch commented May 29, 2022

Thanks @notgull ! I created this repo to try to get things to work and I get the error Error: SetLoggerError(()) when trying to use fern with tracing:

cj@cj-Z:~/projects/macro_sandbox|main ⇒  RUST_BACKTRACE=full cargo run                             
   Compiling macro_sandbox v0.1.0 (/home/cj/projects/macro_sandbox)
    Finished dev [unoptimized + debuginfo] target(s) in 1.11s
     Running `target/debug/macro_sandbox`
Hello, world!
Error: SetLoggerError(())
cj@cj-Z:~/projects/macro_sandbox|main ⇒  

is the log feature in tracing you're referring to the tracing_log crate or a feature flag in tracer?

@notgull
Copy link

notgull commented May 29, 2022

In the repo above, it looks like you initialize a tracing subscriber before you initialize fern. If you don't initialize the tracing subscriber, the code should work.

@cdesch
Copy link

cdesch commented May 29, 2022

Thank you @notgull, it works without the tracing_subscriber, although the tracing_subscriber is needed to instrument the functions. Is it that you either have to choose fern or tracing_subscriber, but not both?

@notgull
Copy link

notgull commented May 29, 2022

Yes. Tracing subscribers put an object in the global log dispatch that redirects log events to tracing. fern trying to add their dispatch system creates an error, because the dispatch slot is already taken.

Consider adding the log-all feature to tracing if you're not seeing all of the events that you'd like to see.

@cdesch
Copy link

cdesch commented May 29, 2022

Thanks @notgull, this really helps!

I'll have to experiment with it a little as I learn more about each library. I got it working a little with removing tracing-log and env-filter from the tracing_subscriber entry in Cargo.toml. This is probably a bit hacky given my rust experience. I pushed the branch workaround-ish to share.

@davidbarsky
Copy link
Member

Following up: I think we can safely close this issue as tracing_subscriber + tracing_appender allow for safely writing logs to a file instead of stderr. While it's possible to use tracing with with log-based loggers (hence why tracing-log exists), I think it's a bit more effort than it's worth, especially when all the code in question is controlled by a single author.

To build off https://github.com/cdesch/macro_sandbox/blob/workaround-ish/src/main.rs, it'd be possible to write to a file using the approach in https://github.com/tokio-rs/tracing/blob/v0.1.x/examples/examples/fmt-multiple-writers.rs. Feel free to open a discussion if you'd like this approach to be elaborated on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants