{"record":{"id":"bcc813037935d0cc","repo":"loco-rs/loco","slug":"logger-initialization-failed","errorCode":null,"errorMessage":"logger initialization failed","messagePattern":"logger initialization failed","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/logger.rs","lineNumber":211,"sourceCode":"    EnvFilter::try_from_default_env()\n        .or_else(|_| {\n            // user wanted a specific filter, don't care about our internal whitelist\n            // or, if no override give them the default whitelisted filter (most common)\n            override_filter.map_or_else(\n                || {\n                    EnvFilter::try_new(\n                        MODULE_WHITELIST\n                            .iter()\n                            .map(|m| format!(\"{m}={level}\"))\n                            .chain(std::iter::once(format!(\"{}={}\", H::app_name(), level)))\n                            .collect::<Vec<_>>()\n                            .join(\",\"),\n                    )\n                },\n                EnvFilter::try_new,\n            )\n        })\n        .expect(\"logger initialization failed\")\n}\n\n/// Builds a single boxed tracing [`Layer`] for `make_writer` in the given\n/// [`Format`], with ANSI colouring toggled by `ansi` — the same layer [`init`]\n/// installs for stdout and the file appender.\n///\n/// Exposed as a building block so an application overriding\n/// [`crate::app::Hooks::init_logger`] can attach Loco's formatted layer to a\n/// custom writer (a socket, an in-memory buffer, a second sink) without\n/// reimplementing the compact/pretty/json formatting choice.\npub fn init_layer<W2>(\n    make_writer: W2,\n    format: &Format,\n    ansi: bool,\n) -> Box<dyn Layer<Registry> + Sync + Send>\nwhere\n    W2: for<'writer> MakeWriter<'writer> + Sync + Send + 'static,\n{","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/loco-rs/loco/blob/23639d1e360dbc618073642b507d6f8664adbaff/src/logger.rs#L193-L229","documentation":"`init_env_filter` builds a `tracing_subscriber::EnvFilter` from the `RUST_LOG` environment variable (with a fallback filter). `EnvFilter::try_new` returns an error when the filter directive string is malformed; the code aggregates failures and `.expect()`s, panicking with 'logger initialization failed' so bad logging configuration fails fast at boot.","triggerScenarios":"Setting `RUST_LOG` (or the app's configured log level) to a syntactically invalid tracing directive such as `RUST_LOG=trace,foo=bar=baz`, an unknown level like `RUST_LOG=verbose`, or a bad regex in a directive; the try_new call then fails and the expect panics.","commonSituations":"Copy-pasting log filters from log4j/logback or other frameworks with incompatible syntax; typos in deployment env files; CI pipelines injecting an empty or garbage `RUST_LOG`; a config YAML level field interpolated to something invalid.","solutions":["Fix the `RUST_LOG` value to valid `tracing_subscriber` directive syntax, e.g. `RUST_LOG=info,my_app=debug`","Use a bare level (`error|warn|info|debug|trace`) or `target=level` pairs only; remove stray commas/equals","Unset `RUST_LOG` entirely to fall back to the library's default filter","Validate the filter in staging with `RUST_LOG=... cargo loco doctor` or a quick `EnvFilter::try_new` probe before deploying"],"exampleFix":"// before\nRUST_LOG=verbose,app=debug=extra\n// after\nRUST_LOG=info,app=debug","handlingStrategy":"validation","validationCode":"// Validate RUST_LOG before initializing\nfn valid_filter(v: &str) -> bool {\n    v.split(',').all(|d| {\n        let d = d.trim();\n        d.is_empty()\n            || matches!(d, \"error\"|\"warn\"|\"info\"|\"debug\"|\"trace\"|\"off\")\n            || d.rsplit_once('=').map_or(!d.is_empty(), |(t, l)| {\n                !t.is_empty() && matches!(l, \"error\"|\"warn\"|\"info\"|\"debug\"|\"trace\"|\"off\")\n            })\n    })\n}\nif let Ok(v) = std::env::var(\"RUST_LOG\") { assert!(valid_filter(&v), \"invalid RUST_LOG: {v}\"); }","typeGuard":null,"tryCatchPattern":"// Probe the filter in your own code before calling loco's init\nmatch tracing_subscriber::EnvFilter::try_new(std::env::var(\"RUST_LOG\").unwrap_or_default()) {\n    Err(e) => eprintln!(\"bad RUST_LOG: {e}; falling back to info\"),\n    Ok(_) => { /* safe to init */ }\n}","preventionTips":["Keep RUST_LOG to level or target=level pairs","Lint env vars in CI before running the app","Document valid filter syntax for your ops team"],"tags":["rust","logging","tracing","env-var","panic"],"backgroundTag":"invalid-env-var-value","analyzedSha":"23639d1e360dbc618073642b507d6f8664adbaff","analyzedAt":"2026-09-12T01:47:20.769Z","contentChangedAt":"2026-09-12T01:47:20.769Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}