Skip to content

Elegant conditional appender? #1588

Answered by hawkw
tumbl3w33d asked this question in Q&A
Sep 23, 2021 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

Try using the implementation of Layer for Option<L: Layer<S>>, like this:

pub(crate) fn init() -> io::Result<Vec<WorkerGuard>> {
    LogTracer::init().expect("Unable to setup log tracer!");

    // collect and hold on to the guards to keep loggers alive
    let mut guards: Vec<WorkerGuard> = Vec::new();

    // always log to stdout
    let (stdout_writer, stdout_writer_guard) = tracing_appender::non_blocking(std::io::stdout());
    guards.push(stdout_writer_guard);
    let stdout_layer = json_layer!(stdout_writer);

    // when LOG_PATH is specified and valid, write logs to a file as well
    let log_path = match std::env::var("LOG_PATH") {
         // ...
    };
 
    let subscriber = Re…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@tumbl3w33d
Comment options

@hawkw
Comment options

Answer selected by tumbl3w33d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants