From d723452ec583774bbbee3b2ebbdf1cd766ce0c52 Mon Sep 17 00:00:00 2001 From: Evan Lloyd New-Schmidt Date: Fri, 15 Mar 2024 12:40:26 -0400 Subject: [PATCH] Use tracing-logfmt Signed-off-by: Evan Lloyd New-Schmidt --- Cargo.lock | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 1 + src/main.rs | 27 ++++++++++++++------ 3 files changed, 89 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ad954f6..65a789f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -301,6 +301,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "deranged" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" +dependencies = [ + "powerfmt", +] + [[package]] name = "derive_more" version = "0.99.17" @@ -679,6 +688,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + [[package]] name = "num_cpus" version = "1.16.0" @@ -718,6 +733,7 @@ dependencies = [ "serde_json", "thiserror", "tracing", + "tracing-logfmt", "tracing-subscriber", "unicode-normalization", "url", @@ -877,6 +893,12 @@ version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -1214,18 +1236,18 @@ checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed" [[package]] name = "serde" -version = "1.0.163" +version = "1.0.193" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2113ab51b87a539ae008b5c6c02dc020ffa39afd2d83cffcb3f4eb2722cebec2" +checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.163" +version = "1.0.193" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c805777e3930c8883389c602315a24224bcc738b63905ef87cd1420353ea93e" +checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" dependencies = [ "proc-macro2", "quote", @@ -1388,6 +1410,37 @@ dependencies = [ "once_cell", ] +[[package]] +name = "time" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8248b6521bb14bc45b4067159b9b6ad792e2d6d754d6c41fb50e29fefe38749" +dependencies = [ + "deranged", + "itoa", + "num-conv", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" + +[[package]] +name = "time-macros" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ba3a3ef41e6672a2f0f001392bb5dcd3ff0a9992d618ca761a11c3121547774" +dependencies = [ + "num-conv", + "time-core", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -1447,6 +1500,18 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-logfmt" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22b8e455f6caa5212a102ec530bf86b8dc5a4c536299bffd84b238fed9119be7" +dependencies = [ + "time", + "tracing", + "tracing-core", + "tracing-subscriber", +] + [[package]] name = "tracing-subscriber" version = "0.3.17" diff --git a/Cargo.toml b/Cargo.toml index 3baec8f..d49f10f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ serde_json = "1.0.96" thiserror = "1.0.44" tracing = "0.1.37" tracing-subscriber = { version = "0.3.17", features = ["env-filter"] } +tracing-logfmt = "0.3.4" url = "2.3.1" urlencoding = "2.1.2" diff --git a/src/main.rs b/src/main.rs index 9c125a9..e1339c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ use anyhow::Context; use clap::{CommandFactory, Parser, Subcommand}; #[macro_use] extern crate tracing; -use tracing_subscriber::filter::EnvFilter; +use tracing_subscriber::{filter::EnvFilter, Layer}; use om_wikiparser::osm; @@ -77,13 +77,7 @@ enum Cmd { } fn main() -> anyhow::Result<()> { - // Use info level by default, load overrides from `RUST_LOG` env variable. - // See https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html - tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .compact() - .with_writer(stderr) - .init(); + init_logger(); let args = Args::parse(); @@ -215,6 +209,23 @@ fn main() -> anyhow::Result<()> { } } +fn init_logger() { + use tracing::dispatcher::{self, Dispatch}; + use tracing_subscriber::{layer::SubscriberExt, Registry}; + + let subscriber = Registry::default().with( + tracing_logfmt::builder() + .layer() + .with_writer(stderr) + // Use info level by default, load overrides from `RUST_LOG` env variable. + // See https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html + .with_filter(EnvFilter::from_default_env()), + ); + + dispatcher::set_global_default(Dispatch::new(subscriber)) + .expect("Global logger has already been set!"); +} + /// Determine the number of threads to use. /// /// If `requested` is <= 0, then the number of cores plus `requested` will be created.