shadowsocks/shadowsocks-rust · error
init logging with file
Error message
init logging with file
What it means
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.
Source
Thrown at src/logging/log4rs.rs:19
//! Logging facilities with log4rs
use std::path::Path;
use log::LevelFilter;
use log4rs::{
append::console::{ConsoleAppender, Target},
config::{Appender, Config, Logger, Root},
encode::pattern::PatternEncoder,
};
use crate::config::LogConfig;
/// Initialize logger ([log4rs](https://crates.io/crates/log4rs)) from yaml configuration file
pub fn init_with_file<P>(path: P)
where
P: AsRef<Path>,
{
log4rs::init_file(path, Default::default()).expect("init logging with file");
}
/// Initialize logger with provided configuration
#[allow(dead_code)]
pub fn init_with_config(bin_name: &str, config: &LogConfig) {
let debug_level = config.level;
let without_time = config.format.without_time;
let mut pattern = String::new();
if !without_time {
pattern += "{d} ";
}
pattern += "{h({l}):<5} ";
if debug_level >= 1 {
pattern += "[{P}:{I}] [{M}] ";
}
pattern += "{m}{n}";
View on GitHub (pinned to 8eb0f0a65b)
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
Example fix
// before
log4rs::init_file(path, Default::default()).expect("init logging with file");
// after
if let Err(e) = log4rs::init_file(&path, Default::default()) {
eprintln!("failed to init logging from {:?}: {}", path, e);
std::process::exit(1);
} Defensive patterns
Strategy: validation
Validate before calling
fn check_log_config(path: &Path) -> Result<(), String> {
let meta = std::fs::metadata(path).map_err(|e| format!("log config unreadable: {}", e))?;
if !meta.is_file() { return Err("log config path is not a file".into()); }
let text = std::fs::read_to_string(path).map_err(|e| format!("log config read failed: {}", e))?;
serde_yaml::from_str::<serde_yaml::Value>(&text).map_err(|e| format!("log config invalid YAML: {}", e))?;
Ok(())
} Try / catch
// init_with_file panics via expect; pre-validate, or patch the caller to match on the Result
match log4rs::init_file(path, Default::default()) {
Ok(_) => {},
Err(e) => eprintln!("log init failed: {}", e),
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Config file not found": what it means and how to fix it in docker-sync, Maven, Vagrant, Turborepo and other tools — this error's family across 60 libraries.
Related errors
- logging
- missing manager config
- Failed to create file writer for logging
- syslog identity contains null-byte ('\0')
- method
AI-assisted analysis of shadowsocks/shadowsocks-rust@8eb0f0a65b (2026-09-09).
Data as JSON: /api/errors/3749ce53508def2f.
Report an issue: GitHub.