influxdata/influxdb · error · Error

Cannot set global log subscriber

Error message

Cannot set global log subscriber

What it means

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.

Solutions

  1. Set the logger only once per process; make init idempotent.
  2. Check `log::set_logger_racy`/set order — call trogging init before any other logger setup.
  3. In tests, guard init with a `Once`/`OnceLock`.

Example fix

// before
fn setup() { trogging::Builder::new().build().unwrap().init().unwrap(); }
#[test] fn a() { setup(); } #[test] fn b() { setup(); } // second fails
// after
static INIT: std::sync::Once = std::sync::Once::new();
fn setup() { INIT.call_once(|| { trogging::Builder::new().build().unwrap().init().unwrap(); }); }
Defensive patterns

Strategy: try-catch

Validate before calling

// log::logger() always returns something; rely on try-init result instead
let _ = trogging::Builder::new().build().init(); // treat SetLoggerError as benign

Try / catch

match trogging::Builder::new().build().init() {
    Err(trogging::Error::SetLoggerError(_)) => { /* logger already set; continue */ }
    other => other?,
}

Prevention

When it happens

Trigger: 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.

Common situations: Double initialization of logging in one process; combining trogging with another crate that calls `log::set_logger`; tests each attempting logger setup.

Related errors


AI-assisted analysis of influxdata/influxdb@06200ef96b (2026-09-19). Data as JSON: /api/errors/7c1fad469014d46a. Report an issue: GitHub.

Appendix: source

Thrown at core/trogging/src/lib.rs:40

    registry::LookupSpan,
};

/// Maximum length of a log line.
/// Space for a final trailing newline if truncated.
///
/// Docker "chunks" log message in 16KB chunks. The log driver receives log lines in such chunks
/// but not all of the log drivers properly recombine the lines, and even those who do have their
/// own max buffer sizes (e.g. for our fluentd config it's 32KB).
/// To avoid surprises, for now, let's just truncate log lines right below 16K and make sure
/// they are properly terminated with a newline if they were so before the truncation.
const MAX_LINE_LENGTH: usize = 16 * 1024 - 1;

#[derive(Debug, Error)]
pub enum Error {
    #[error("Cannot set global tracing subscriber")]
    SetGlobalDefaultError(#[from] tracing::dispatcher::SetGlobalDefaultError),

    #[error("Cannot set global log subscriber")]
    SetLoggerError(#[from] tracing_log::log_tracer::SetLoggerError),

    #[error("`tokio_console` feature not enabled")]
    TokioConsoleMissing,
}

pub type Result<T, E = Error> = std::result::Result<T, E>;

/// Builder to configure tracing and logging.
#[derive(Debug)]
pub struct Builder<W = fn() -> io::Stdout> {
    log_format: LogFormat,
    log_filter: Option<EnvFilter>,
    // used when log_filter is none.
    default_log_filter: EnvFilter,
    make_writer: W,
    with_target: bool,
    with_ansi: bool,

View on GitHub (pinned to 06200ef96b)