Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds option to emit json structured logs #245

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 43 additions & 28 deletions crates/librqbit/src/tracing_subscriber_config_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ use anyhow::Context;
use bytes::Bytes;
use librqbit_core::spawn_utils::spawn;
use tracing::error_span;
use tracing_subscriber::fmt::MakeWriter;
use tracing_subscriber::Layer;
use tracing_subscriber::{
fmt::{self, MakeWriter},
prelude::*,
EnvFilter,
};

struct Subscriber {
tx: tokio::sync::broadcast::Sender<Bytes>,
Expand Down Expand Up @@ -53,6 +58,8 @@ 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>,
scottopell marked this conversation as resolved.
Show resolved Hide resolved
pub log_file_json: bool,
pub log_json: bool,
}

pub struct InitLoggingResult {
Expand All @@ -62,7 +69,7 @@ pub struct InitLoggingResult {

#[inline(never)]
pub fn init_logging(opts: InitLoggingOptions) -> anyhow::Result<InitLoggingResult> {
let stderr_filter = EnvFilter::builder()
let stdout_filter = EnvFilter::builder()
.with_default_directive(
opts.default_rust_log_value
.unwrap_or("info")
Expand All @@ -72,25 +79,25 @@ pub fn init_logging(opts: InitLoggingOptions) -> anyhow::Result<InitLoggingResul
.from_env()
.context("invalid RUST_LOG value")?;

let (stderr_filter, reload_stderr_filter) =
tracing_subscriber::reload::Layer::new(stderr_filter);
let (filter_layer, reload_handle) = tracing_subscriber::reload::Layer::new(stdout_filter);
let (line_sub, line_broadcast) = Subscriber::new();

use tracing_subscriber::{fmt, prelude::*, EnvFilter};
let stdout_layer: Box<dyn Layer<_> + Send + Sync> = if opts.log_json {
Box::new(fmt::layer().json())
} else {
Box::new(fmt::layer())
};

let (line_sub, line_broadcast) = Subscriber::new();
let http_api_log_broadcast_layer = fmt::layer()
.with_ansi(false)
.fmt_fields(tracing_subscriber::fmt::format::JsonFields::new())
.event_format(fmt::format().with_ansi(false).json())
.with_writer(line_sub)
.with_filter(EnvFilter::builder().parse("info,librqbit=debug").unwrap());

let layered = tracing_subscriber::registry()
// Stderr logging layer.
.with(fmt::layer().with_filter(stderr_filter))
// HTTP API log broadcast layer.
.with(
fmt::layer()
.with_ansi(false)
.fmt_fields(tracing_subscriber::fmt::format::JsonFields::new())
.event_format(fmt::format().with_ansi(false).json())
.with_writer(line_sub)
.with_filter(EnvFilter::builder().parse("info,librqbit=debug").unwrap()),
);
.with(stdout_layer.with_filter(filter_layer))
.with(http_api_log_broadcast_layer);

#[cfg(feature = "tokio-console")]
let console_layer = console_subscriber::spawn();
Expand All @@ -107,35 +114,43 @@ pub fn init_logging(opts: InitLoggingOptions) -> anyhow::Result<InitLoggingResul
.open(&log_file)
.with_context(|| format!("error opening log file {:?}", log_file))?,
));
layered
.with(
let log_env_filter = EnvFilter::builder()
.parse(opts.log_file_rust_log.unwrap_or("info,librqbit=debug"))
.context("can't parse log-file-rust-log")?;
let log_layer: Box<dyn Layer<_> + Send + Sync> = if opts.log_file_json {
Box::new(
fmt::layer()
.with_ansi(false)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this makes log file contain ANSI escape codes which makes it hard to grep and use. Those escape codes are intended for terminal use only.

PR LGTM otherwise, I'd merge if not for this

.json()
.with_writer(log_file)
.with_filter(
EnvFilter::builder()
.parse(opts.log_file_rust_log.unwrap_or("info,librqbit=debug"))
.context("can't parse log-file-rust-log")?,
),
.with_filter(log_env_filter),
)
} else {
Box::new(
fmt::layer()
.with_writer(log_file)
.with_filter(log_env_filter),
)
};
layered
.with(log_layer)
.try_init()
.context("can't init logging")?;
.context("Can't init file logging")?;
} else {
layered.try_init().context("can't init logging")?;
}

let (reload_tx, mut reload_rx) = tokio::sync::mpsc::unbounded_channel::<String>();
spawn(error_span!("fmt_filter_reloader"), async move {
while let Some(rust_log) = reload_rx.recv().await {
let stderr_env_filter = match EnvFilter::builder().parse(&rust_log) {
let stdout_env_filter = match EnvFilter::builder().parse(&rust_log) {
Ok(f) => f,
Err(e) => {
eprintln!("can't parse env filter {:?}: {:#?}", rust_log, e);
continue;
}
};
eprintln!("setting RUST_LOG to {:?}", rust_log);
let _ = reload_stderr_filter.reload(stderr_env_filter);
let _ = reload_handle.reload(stdout_env_filter);
}
Ok(())
});
Expand Down
10 changes: 10 additions & 0 deletions crates/rqbit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ struct Opts {
)]
log_file_rust_log: String,

/// Logging to console (ie, stdout) will emit each log record as a json object
#[arg(long = "log-json", env = "RQBIT_LOG_JSON")]
log_json: bool,

/// Log file will contain json-formatted log records
#[arg(long = "log-file-json", env = "RQBIT_LOG_FILE_JSON")]
log_file_json: 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 @@ -438,6 +446,8 @@ async fn async_main(opts: Opts, cancel: CancellationToken) -> anyhow::Result<()>
}),
log_file: opts.log_file.as_deref(),
log_file_rust_log: Some(&opts.log_file_rust_log),
log_file_json: opts.log_file_json,
log_json: opts.log_json,
})?;

match librqbit::try_increase_nofile_limit() {
Expand Down
2 changes: 2 additions & 0 deletions desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ async fn start() {
default_rust_log_value: Some("info"),
log_file: None,
log_file_rust_log: None,
log_file_json: false,
log_json: false,
})
.unwrap();

Expand Down
Loading