-
Notifications
You must be signed in to change notification settings - Fork 129
Adds option to emit json structured logs #245
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
Open
scottopell
wants to merge
7
commits into
ikatson:main
Choose a base branch
from
scottopell:sopell/structured-stdout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3583c6e
Adds json log output format
scottopell 3265d2c
Finally success?
scottopell ca00252
slightly simplify types and names
scottopell e2bd9fd
Use dyn trick to remove code dup in log file path
scottopell 4e9ca8e
extract http api layer out into var
scottopell 1cf9c7c
Fixes logic bugs introduced during copy-pasta
scottopell 740de5a
Merge branch 'main' into sopell/structured-stdout
ikatson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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>, | ||
|
|
@@ -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>, | ||
| pub log_file_json: bool, | ||
| pub log_json: bool, | ||
| } | ||
|
|
||
| pub struct InitLoggingResult { | ||
|
|
@@ -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") | ||
|
|
@@ -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(); | ||
|
|
@@ -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) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(()) | ||
| }); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.