Skip to content

Commit

Permalink
feat: change std to stdout && refactor init_subscriber
Browse files Browse the repository at this point in the history
Signed-off-by: GFX9 <[email protected]>
  • Loading branch information
GFX9 authored and Phoenix500526 committed Apr 25, 2024
1 parent be2da2f commit 1744939
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 40 deletions.
2 changes: 1 addition & 1 deletion crates/xline/src/utils/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())]
Expand Down
65 changes: 26 additions & 39 deletions crates/xline/src/utils/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::io::Write + Send> {
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
Expand Down Expand Up @@ -43,47 +57,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))
}

0 comments on commit 1744939

Please sign in to comment.