fix: don't log tracing messages from other crates

This commit is contained in:
Einliterflasche 2024-08-20 14:16:54 +02:00
parent b060c75cc7
commit 853164b7c7
2 changed files with 13 additions and 8 deletions

View file

@ -189,7 +189,7 @@ impl Context {
let format = if json { Format::Json } else { Format::Raw };
let level_filter = if debug {
LevelFilter::from_level(Level::DEBUG)
LevelFilter::from_level(Level::DEBUG)
} else {
LevelFilter::from_level(Level::INFO)
};

View file

@ -1,11 +1,11 @@
use std::path::Path;
use std::str::FromStr;
use anyhow::Result;
use tracing::Level;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::filter::{Directive, LevelFilter};
use tracing_subscriber::fmt::time::UtcTime;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::{fmt, Layer};
use tracing_subscriber::{fmt, EnvFilter, Layer};
use tracing_subscriber::util::SubscriberInitExt;
/// Output formats for logging messages.
@ -25,9 +25,14 @@ pub fn init(
format: Format,
dir: impl AsRef<Path>,
) -> Result<()> {
if level_filter == LevelFilter::OFF {
return Ok(());
}
let env_filter = EnvFilter::from_default_env()
.add_directive(Directive::from_str(
&format!(
"asb={}",
&level_filter,
)
)?)
.add_directive(Directive::from_str(&format!("swap={}", &level_filter))?);
// file logger will always write in JSON format and with timestamps
let file_appender = tracing_appender::rolling::never(&dir, "swap-all.log");
@ -38,7 +43,7 @@ pub fn init(
.with_timer(UtcTime::rfc_3339())
.with_target(false)
.json()
.with_filter(LevelFilter::from_level(Level::DEBUG));
.with_filter(env_filter);
// terminal logger
let is_terminal = atty::is(atty::Stream::Stderr);