Skip to content

Commit

Permalink
Adds option to emit json structured logs to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
scottopell committed Sep 28, 2024
1 parent c291e42 commit b3ca2e0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
23 changes: 21 additions & 2 deletions crates/librqbit/src/tracing_subscriber_config_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ impl std::io::Write for Writer {
pub struct InitLoggingOptions<'a> {
pub default_rust_log_value: Option<&'a str>,
pub log_file: Option<&'a str>,
pub log_file_rust_log: Option<&'a str>,
pub log_file_rust_log: &'a str,
pub structured_stdout: bool,
}

pub struct InitLoggingResult {
Expand Down Expand Up @@ -107,7 +108,25 @@ pub fn init_logging(opts: InitLoggingOptions) -> anyhow::Result<InitLoggingResul
.with_writer(log_file)
.with_filter(
EnvFilter::builder()
.parse(opts.log_file_rust_log.unwrap_or("info,librqbit=debug"))
.parse(opts.log_file_rust_log)
.context("can't parse log-file-rust-log")?,
),
)
.try_init()
.context("can't init logging")?;
} else if opts.structured_stdout {
// Use JSON logging to stdout
layered
.with(
fmt::layer()
.with_ansi(false)
.json()
.with_current_span(true)
.with_target(true)
.with_writer(std::io::stdout)
.with_filter(
EnvFilter::builder()
.parse(opts.log_file_rust_log)
.context("can't parse log-file-rust-log")?,
),
)
Expand Down
7 changes: 6 additions & 1 deletion crates/rqbit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ struct Opts {
#[arg(long = "log-file-rust-log", default_value = "librqbit=trace,info")]
log_file_rust_log: String,

/// Use structured logging when emitting to stdout
#[arg(long = "structured-stdout", default_value_t = false)]
structured_stdout: bool,

/// The interval to poll trackers, e.g. 30s.
/// Trackers send the refresh interval when we connect to them. Often this is
/// pretty big, e.g. 30 minutes. This can force a certain value.
Expand Down Expand Up @@ -251,7 +255,8 @@ async fn async_main(opts: Opts) -> anyhow::Result<()> {
LogLevel::Error => "error",
}),
log_file: opts.log_file.as_deref(),
log_file_rust_log: Some(&opts.log_file_rust_log),
log_file_rust_log: &opts.log_file_rust_log,
structured_stdout: opts.structured_stdout,
})?;

match librqbit::try_increase_nofile_limit() {
Expand Down

0 comments on commit b3ca2e0

Please sign in to comment.