diff --git a/crates/xline/src/utils/args.rs b/crates/xline/src/utils/args.rs index 69b6b79c8..2ec9de836 100644 --- a/crates/xline/src/utils/args.rs +++ b/crates/xline/src/utils/args.rs @@ -92,7 +92,7 @@ pub struct ServerArgs { #[clap(long, value_parser = parse_metrics_push_protocol, default_value_t = default_metrics_push_protocol())] metrics_push_protocol: MetricsPushProtocol, /// Log file path - #[clap(long, default_value = "/std")] + #[clap(long, default_value = "/stdout")] log_file: PathBuf, /// Log rotate strategy, eg: never, hourly, daily #[clap(long, value_parser = parse_rotation, default_value_t = default_rotation())] diff --git a/crates/xline/src/utils/trace.rs b/crates/xline/src/utils/trace.rs index 9dc3c2857..775b2256c 100644 --- a/crates/xline/src/utils/trace.rs +++ b/crates/xline/src/utils/trace.rs @@ -6,6 +6,20 @@ use tracing_appender::non_blocking::WorkerGuard; use tracing_subscriber::{fmt::format, layer::SubscriberExt, util::SubscriberInitExt, Layer}; use utils::config::{default_rotation, file_appender, LogConfig, TraceConfig}; +/// Return a Box trait from the config +fn generate_writer(name: &str, log_config: &LogConfig) -> Box { + match *log_config.path() { + Some(ref file_path) if file_path.to_string_lossy() == "/stdout" => { + if *log_config.rotation() != default_rotation() { + warn!("The log is output to the terminal, so the rotation parameter is ignored."); + } + Box::new(std::io::stdout()) + } + Some(ref file_path) => Box::new(file_appender(*log_config.rotation(), file_path, name)), + None => unreachable!("the path of log cannot be empty in config"), + } +} + /// init tracing subscriber /// # Errors /// Return error if init failed @@ -41,47 +55,20 @@ pub fn init_subscriber( .install_batch(), ) }); - let jaeger_fmt_layer = tracing_subscriber::fmt::layer() .with_filter(tracing_subscriber::EnvFilter::from_default_env()); - - let prev_layers = tracing_subscriber::registry() + let writer = generate_writer(name, log_config); + let (non_blocking, guard) = tracing_appender::non_blocking(writer); + let log_layer = tracing_subscriber::fmt::layer() + .event_format(format().compact()) + .with_writer(non_blocking) + .with_ansi(false) + .with_filter(*log_config.level()); + tracing_subscriber::registry() .with(jaeger_fmt_layer) .with(jaeger_online_layer) - .with(jaeger_offline_layer); - - match *log_config.path() { - Some(ref file_path) => { - if file_path.to_string_lossy() == "/std" { - if *log_config.rotation() != default_rotation() { - warn!( - "The log is output to the terminal, so the rotation parameter is ignored." - ); - } - let stdout_layer = tracing_subscriber::fmt::layer() - .event_format(format().compact()) - .with_writer(std::io::stderr) - .with_ansi(false) - .with_filter(*log_config.level()); - - prev_layers.with(stdout_layer).try_init()?; - return Ok(None); - } - let mut guard = None; - let log_file_layer = log_config.path().as_ref().map(|log_path| { - let file_appender = file_appender(*log_config.rotation(), log_path, name); - // `WorkerGuard` should be assigned in the `main` function or whatever the entrypoint of the program is. - let (non_blocking, guard_inner) = tracing_appender::non_blocking(file_appender); - guard = Some(guard_inner); - tracing_subscriber::fmt::layer() - .event_format(format().compact()) - .with_writer(non_blocking) - .with_ansi(false) - .with_filter(*log_config.level()) - }); - prev_layers.with(log_file_layer).try_init()?; - Ok(guard) - } - None => unreachable!(), - } + .with(jaeger_offline_layer) + .with(log_layer) + .try_init()?; + Ok(Some(guard)) }