{"record":{"id":"3749ce53508def2f","repo":"shadowsocks/shadowsocks-rust","slug":"init-logging-with-file","errorCode":null,"errorMessage":"init logging with file","messagePattern":"init logging with file","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/logging/log4rs.rs","lineNumber":19,"sourceCode":"//! Logging facilities with log4rs\n\nuse std::path::Path;\n\nuse log::LevelFilter;\nuse log4rs::{\n    append::console::{ConsoleAppender, Target},\n    config::{Appender, Config, Logger, Root},\n    encode::pattern::PatternEncoder,\n};\n\nuse crate::config::LogConfig;\n\n/// Initialize logger ([log4rs](https://crates.io/crates/log4rs)) from yaml configuration file\npub fn init_with_file<P>(path: P)\nwhere\n    P: AsRef<Path>,\n{\n    log4rs::init_file(path, Default::default()).expect(\"init logging with file\");\n}\n\n/// Initialize logger with provided configuration\n#[allow(dead_code)]\npub fn init_with_config(bin_name: &str, config: &LogConfig) {\n    let debug_level = config.level;\n    let without_time = config.format.without_time;\n\n    let mut pattern = String::new();\n    if !without_time {\n        pattern += \"{d} \";\n    }\n    pattern += \"{h({l}):<5} \";\n    if debug_level >= 1 {\n        pattern += \"[{P}:{I}] [{M}] \";\n    }\n    pattern += \"{m}{n}\";\n","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/shadowsocks/shadowsocks-rust/blob/8eb0f0a65b1d976ab6bed5787327ef86529b0435/src/logging/log4rs.rs#L1-L37","documentation":"Panic from shadowsocks-rust's log4rs logging init: log4rs::init_file(path, ...) failed while loading the YAML logging configuration file. Common causes are a missing file, invalid YAML, or a config referencing unknown appenders/encoders in the deserializer.","triggerScenarios":"Calling logging::log4rs::init_with_file(path) where the path doesn't exist, is unreadable, or contains a YAML document that doesn't deserialize as a log4rs Config.","commonSituations":"Wrong log-config path passed via CLI/launch script; hand-edited log4rs.yaml with a typo or unsupported key; working-directory differences making a relative path resolve elsewhere in daemonized runs.","solutions":["Verify the config file exists and is readable at the given path (use an absolute path)","Validate the YAML with a log4rs example config; fix unknown/misspelled keys","Check that the required log4rs features (e.g. yaml_format) are enabled in the build","Pre-check the file before init and exit with a friendly message instead of panicking"],"exampleFix":"// before\nlog4rs::init_file(path, Default::default()).expect(\"init logging with file\");\n// after\nif let Err(e) = log4rs::init_file(&path, Default::default()) {\n    eprintln!(\"failed to init logging from {:?}: {}\", path, e);\n    std::process::exit(1);\n}","handlingStrategy":"validation","validationCode":"fn check_log_config(path: &Path) -> Result<(), String> {\n    let meta = std::fs::metadata(path).map_err(|e| format!(\"log config unreadable: {}\", e))?;\n    if !meta.is_file() { return Err(\"log config path is not a file\".into()); }\n    let text = std::fs::read_to_string(path).map_err(|e| format!(\"log config read failed: {}\", e))?;\n    serde_yaml::from_str::<serde_yaml::Value>(&text).map_err(|e| format!(\"log config invalid YAML: {}\", e))?;\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":"// init_with_file panics via expect; pre-validate, or patch the caller to match on the Result\nmatch log4rs::init_file(path, Default::default()) {\n    Ok(_) => {},\n    Err(e) => eprintln!(\"log init failed: {}\", e),\n}","preventionTips":["Use absolute paths for logging config files, especially when daemonizing (cwd changes)","Keep log4rs.yaml under version control and lint it in CI","Verify required log4rs features (yaml_format) are enabled at build time","Validate YAML syntax before deployment"],"tags":["rust","logging","log4rs","yaml","config"],"backgroundTag":"config-file-not-found","analyzedSha":"8eb0f0a65b1d976ab6bed5787327ef86529b0435","analyzedAt":"2026-09-09T12:20:43.168Z","contentChangedAt":"2026-09-09T12:20:43.168Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}