{"record":{"id":"7c1fad469014d46a","repo":"influxdata/influxdb","slug":"cannot-set-global-log-subscriber","errorCode":null,"errorMessage":"Cannot set global log subscriber","messagePattern":"Cannot set global log subscriber","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"core/trogging/src/lib.rs","lineNumber":40,"sourceCode":"    registry::LookupSpan,\n};\n\n/// Maximum length of a log line.\n/// Space for a final trailing newline if truncated.\n///\n/// Docker \"chunks\" log message in 16KB chunks. The log driver receives log lines in such chunks\n/// but not all of the log drivers properly recombine the lines, and even those who do have their\n/// own max buffer sizes (e.g. for our fluentd config it's 32KB).\n/// To avoid surprises, for now, let's just truncate log lines right below 16K and make sure\n/// they are properly terminated with a newline if they were so before the truncation.\nconst MAX_LINE_LENGTH: usize = 16 * 1024 - 1;\n\n#[derive(Debug, Error)]\npub enum Error {\n    #[error(\"Cannot set global tracing subscriber\")]\n    SetGlobalDefaultError(#[from] tracing::dispatcher::SetGlobalDefaultError),\n\n    #[error(\"Cannot set global log subscriber\")]\n    SetLoggerError(#[from] tracing_log::log_tracer::SetLoggerError),\n\n    #[error(\"`tokio_console` feature not enabled\")]\n    TokioConsoleMissing,\n}\n\npub type Result<T, E = Error> = std::result::Result<T, E>;\n\n/// Builder to configure tracing and logging.\n#[derive(Debug)]\npub struct Builder<W = fn() -> io::Stdout> {\n    log_format: LogFormat,\n    log_filter: Option<EnvFilter>,\n    // used when log_filter is none.\n    default_log_filter: EnvFilter,\n    make_writer: W,\n    with_target: bool,\n    with_ansi: bool,","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/influxdata/influxdb/blob/06200ef96ba82c5f6727e5038a83af8e722c6875/core/trogging/src/lib.rs#L22-L58","documentation":"A trogging `Error` variant wrapping `tracing_log::log_tracer::SetLoggerError`. It fires when the `log` crate's global logger cannot be set, which happens when a logger was already installed. Trogging bridges `log` records into tracing, so it installs the LogTracer during init and surfaces this error if that fails.","triggerScenarios":"Initializing trogging (which installs LogTracer via `log::set_logger`) when a global `log` logger is already set, or init is called after the logger was previously installed.","commonSituations":"Double initialization of logging in one process; combining trogging with another crate that calls `log::set_logger`; tests each attempting logger setup.","solutions":["Set the logger only once per process; make init idempotent.","Check `log::set_logger_racy`/set order — call trogging init before any other logger setup.","In tests, guard init with a `Once`/`OnceLock`."],"exampleFix":"// before\nfn setup() { trogging::Builder::new().build().unwrap().init().unwrap(); }\n#[test] fn a() { setup(); } #[test] fn b() { setup(); } // second fails\n// after\nstatic INIT: std::sync::Once = std::sync::Once::new();\nfn setup() { INIT.call_once(|| { trogging::Builder::new().build().unwrap().init().unwrap(); }); }","handlingStrategy":"try-catch","validationCode":"// log::logger() always returns something; rely on try-init result instead\nlet _ = trogging::Builder::new().build().init(); // treat SetLoggerError as benign","typeGuard":null,"tryCatchPattern":"match trogging::Builder::new().build().init() {\n    Err(trogging::Error::SetLoggerError(_)) => { /* logger already set; continue */ }\n    other => other?,\n}","preventionTips":["Call trogging init before any crate that installs a log logger","Guard init with std::sync::Once in libraries and tests","Avoid mixing trogging with other log::set_logger callers in one process"],"tags":["rust","logging","log-crate","initialization"],"backgroundTag":"module-init-failed","analyzedSha":"06200ef96ba82c5f6727e5038a83af8e722c6875","analyzedAt":"2026-09-19T12:55:30.003Z","contentChangedAt":"2026-09-19T12:55:30.003Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}