diff --git a/Cargo.lock b/Cargo.lock index 64d9768..05aa786 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -837,6 +837,16 @@ dependencies = [ "tempfile", ] +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num-traits" version = "0.2.17" @@ -1045,6 +1055,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "percent-encoding" version = "2.2.0" @@ -1813,6 +1829,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" dependencies = [ "matchers", + "nu-ansi-term", "once_cell", "regex", "serde", diff --git a/Cargo.toml b/Cargo.toml index 5403445..9dab5cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ tracing-subscriber = { version = "0.3.18", default-features = false, features = "env-filter", "fmt", "json", + "ansi" ] } serde = { version = "1.0.195", features = ["derive"], optional = true } serde_json = { version = "1.0.111", optional = true } diff --git a/src/lib.rs b/src/lib.rs index 8a541eb..885e5a0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,8 +2,6 @@ // which is licensed under CC0 1.0 Universal // https://github.com/davidB/tracing-opentelemetry-instrumentation-sdk/blob/d3609ac2cc699d3a24fbf89754053cc8e938e3bf/LICENSE -use std::cmp::max; - use opentelemetry_sdk::{ propagation::{ BaggagePropagator, TextMapCompositePropagator, TraceContextPropagator, @@ -11,12 +9,14 @@ use opentelemetry_sdk::{ resource::{OsResourceDetector, ResourceDetector}, Resource, }; +use std::cmp::max; +use tracing::Subscriber; +use tracing_subscriber::fmt::writer::MakeWriterExt; use opentelemetry::{propagation::TextMapPropagator, trace::TraceError}; use tracing_subscriber::{ - filter::LevelFilter, - fmt::{format::FmtSpan, writer::MakeWriterExt}, - layer::SubscriberExt, + filter::LevelFilter, fmt::format::FmtSpan, layer::SubscriberExt, + registry::LookupSpan, Layer, }; pub mod middleware; @@ -109,6 +109,32 @@ impl ResourceDetector for ServiceInfoDetector { } } +pub fn build_logger_text( + log_level: tracing::Level, +) -> Box + Send + Sync + 'static> +where + S: Subscriber + for<'a> LookupSpan<'a>, +{ + if cfg!(debug_assertions) { + Box::new( + tracing_subscriber::fmt::layer() + .pretty() + .with_line_number(true) + .with_thread_names(true) + .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE) + .with_timer(tracing_subscriber::fmt::time::uptime()), + ) + } else { + Box::new( + tracing_subscriber::fmt::layer() + .json() + .with_timer(tracing_subscriber::fmt::time::uptime()) + .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE) + .with_writer(std::io::stdout.with_max_level(log_level)), + ) + } +} + pub fn init_tracing_with_fallbacks( log_level: tracing::Level, fallback_service_name: &'static str, @@ -119,17 +145,18 @@ pub fn init_tracing_with_fallbacks( let otel_tracer = otlp::init_tracer(otel_rsrc, otlp::identity).expect("setup of Tracer"); init_propagator().expect("setup of propagator"); - let otel_layer = tracing_opentelemetry::layer().with_tracer(otel_tracer); - - let fmt_layer = tracing_subscriber::fmt::layer() - .json() - .with_timer(tracing_subscriber::fmt::time::uptime()) - .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE) - .with_writer(std::io::stdout.with_max_level(log_level)); let level_filter: LevelFilter = max(log_level, tracing::Level::INFO).into(); let subscriber = tracing_subscriber::registry() - .with(fmt_layer) + .with(level_filter) + .with(build_logger_text(log_level)); + let _guard = tracing::subscriber::set_default(subscriber); + tracing::info!("init logging & tracing"); + + let otel_layer = tracing_opentelemetry::layer().with_tracer(otel_tracer); + + let subscriber = tracing_subscriber::registry() + .with(build_logger_text(log_level)) .with(otel_layer) .with(level_filter); tracing::subscriber::set_global_default(subscriber).unwrap();