Skip to content
Merged
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
16 changes: 9 additions & 7 deletions crates/tower-telemetry/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ macro_rules! error {
}

/// LogLevel describes the various log levels that can be used in the application.
#[derive(PartialEq, PartialOrd)]
pub enum LogLevel {
Debug,
Info,
Expand Down Expand Up @@ -178,16 +179,17 @@ fn should_use_color(destination: &LogDestination) -> bool {
}
}

fn create_fmt_layer(format: LogFormat, destination: LogDestination) -> BoxedFmtLayer {
fn create_fmt_layer(level: &LogLevel, format: LogFormat, destination: LogDestination) -> BoxedFmtLayer {
let use_color = should_use_color(&destination);
let with_target = *level < LogLevel::Warn;

match destination {
LogDestination::Stdout => match format {
LogFormat::Plain => Box::new(
fmt::layer().event_format(
fmt::format()
.pretty()
.with_target(false)
.with_target(with_target)
.with_file(false)
.with_line_number(false)
.with_ansi(use_color),
Expand All @@ -197,7 +199,7 @@ fn create_fmt_layer(format: LogFormat, destination: LogDestination) -> BoxedFmtL
fmt::layer().event_format(
fmt::format()
.json()
.with_target(false)
.with_target(with_target)
.with_file(false)
.with_line_number(false)
.with_ansi(use_color),
Expand All @@ -212,7 +214,7 @@ fn create_fmt_layer(format: LogFormat, destination: LogDestination) -> BoxedFmtL
.event_format(
fmt::format()
.pretty()
.with_target(false)
.with_target(with_target)
.with_file(false)
.with_line_number(false)
.with_ansi(use_color),
Expand All @@ -224,7 +226,7 @@ fn create_fmt_layer(format: LogFormat, destination: LogDestination) -> BoxedFmtL
.event_format(
fmt::format()
.json()
.with_target(false)
.with_target(with_target)
.with_file(false)
.with_line_number(false)
.with_ansi(use_color),
Expand All @@ -237,13 +239,13 @@ fn create_fmt_layer(format: LogFormat, destination: LogDestination) -> BoxedFmtL
}

pub fn enable_logging(level: LogLevel, format: LogFormat, destination: LogDestination) {
let filter = EnvFilter::new(level)
let filter = EnvFilter::new(&level)
.add_directive("h2=off".parse().unwrap())
.add_directive("tower::buffer=off".parse().unwrap())
.add_directive("hyper_util=off".parse().unwrap());

let subscriber = tracing_subscriber::registry()
.with(create_fmt_layer(format, destination))
.with(create_fmt_layer(&level, format, destination))
.with(filter);

let _ = tracing::subscriber::set_global_default(subscriber);
Expand Down
Loading