Skip to content

Commit

Permalink
optionally use JSON format for logs
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Aug 12, 2024
1 parent a6435f1 commit 40dd7e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ sentry-tower = { version = "0.34.0", features = ["http"] }
sentry-anyhow = { version = "0.34.0", features = ["backtrace"] }
log = "0.4"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", default-features = false, features = ["ansi", "fmt", "env-filter", "tracing-log"] }
tracing-subscriber = { version = "0.3.16", default-features = false, features = ["ansi", "fmt", "json", "env-filter", "tracing-log"] }
tracing-log = "0.2.0"
regex = "1"
clap = { version = "4.0.22", features = [ "derive" ] }
Expand Down
24 changes: 16 additions & 8 deletions src/bin/cratesfyi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,22 @@ fn main() {
// through rustwide.
rustwide::logging::init_with(LogTracer::new());

let tracing_registry = tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.with(
EnvFilter::builder()
.with_default_directive(Directive::from_str("docs_rs=info").unwrap())
.with_env_var("DOCSRS_LOG")
.from_env_lossy(),
);
let log_formatter = {
let log_format = env::var("DOCSRS_LOG_FORMAT").unwrap_or_default();

if log_format == "json" {
tracing_subscriber::fmt::layer().json().boxed()
} else {
tracing_subscriber::fmt::layer().boxed()
}
};

let tracing_registry = tracing_subscriber::registry().with(log_formatter).with(
EnvFilter::builder()
.with_default_directive(Directive::from_str("docs_rs=info").unwrap())
.with_env_var("DOCSRS_LOG")
.from_env_lossy(),
);

let _sentry_guard = if let Ok(sentry_dsn) = env::var("SENTRY_DSN") {
tracing::subscriber::set_global_default(tracing_registry.with(
Expand Down

0 comments on commit 40dd7e0

Please sign in to comment.