fix debug

This commit is contained in:
John Smith 2022-06-08 11:45:30 -04:00
parent 38abe7874d
commit 69d68e900e
2 changed files with 38 additions and 25 deletions

View File

@ -177,6 +177,24 @@ impl VeilidLogLevel {
log::Level::Trace => VeilidLogLevel::Trace, log::Level::Trace => VeilidLogLevel::Trace,
} }
} }
pub fn to_tracing_level(&self) -> tracing::Level {
match self {
Self::Error => tracing::Level::ERROR,
Self::Warn => tracing::Level::WARN,
Self::Info => tracing::Level::INFO,
Self::Debug => tracing::Level::DEBUG,
Self::Trace => tracing::Level::TRACE,
}
}
pub fn to_log_level(&self) -> log::Level {
match self {
Self::Error => log::Level::Error,
Self::Warn => log::Level::Warn,
Self::Info => log::Level::Info,
Self::Debug => log::Level::Debug,
Self::Trace => log::Level::Trace,
}
}
} }
impl fmt::Display for VeilidLogLevel { impl fmt::Display for VeilidLogLevel {

View File

@ -10,17 +10,7 @@ pub struct VeilidLogs {
pub guard: Option<non_blocking::WorkerGuard>, pub guard: Option<non_blocking::WorkerGuard>,
} }
fn logfilter<T: AsRef<str>, V: AsRef<[T]>>( fn logfilter<T: AsRef<str>, V: AsRef<[T]>>(metadata: &Metadata, ignore_list: V) -> bool {
metadata: &Metadata,
max_level: veilid_core::VeilidLogLevel,
ignore_list: V,
) -> bool {
// Skip things out of level
let log_level = veilid_core::VeilidLogLevel::from_tracing_level(*metadata.level());
if log_level <= max_level {
return true;
}
// Skip filtered targets // Skip filtered targets
!match (metadata.target(), ignore_list.as_ref()) { !match (metadata.target(), ignore_list.as_ref()) {
(path, ignore) if !ignore.is_empty() => { (path, ignore) if !ignore.is_empty() => {
@ -44,21 +34,19 @@ impl VeilidLogs {
let subscriber = Registry::default(); let subscriber = Registry::default();
let subscriber = subscriber.with(
EnvFilter::builder()
.with_default_directive(level_filters::LevelFilter::INFO.into())
.from_env_lossy(),
);
let subscriber = subscriber.with(if settingsr.logging.terminal.enabled { let subscriber = subscriber.with(if settingsr.logging.terminal.enabled {
let terminal_max_log_level = convert_loglevel(settingsr.logging.terminal.level); let terminal_max_log_level: level_filters::LevelFilter =
convert_loglevel(settingsr.logging.terminal.level)
.to_tracing_level()
.into();
let ignore_list = ignore_list.clone(); let ignore_list = ignore_list.clone();
Some( Some(
fmt::Layer::new() fmt::Layer::new()
.compact() .compact()
.with_writer(std::io::stdout) .with_writer(std::io::stdout)
.with_filter(terminal_max_log_level)
.with_filter(filter::FilterFn::new(move |metadata| { .with_filter(filter::FilterFn::new(move |metadata| {
logfilter(metadata, terminal_max_log_level, &ignore_list) logfilter(metadata, &ignore_list)
})), })),
) )
} else { } else {
@ -67,7 +55,10 @@ impl VeilidLogs {
let mut guard = None; let mut guard = None;
let subscriber = subscriber.with(if settingsr.logging.file.enabled { let subscriber = subscriber.with(if settingsr.logging.file.enabled {
let file_max_log_level = convert_loglevel(settingsr.logging.file.level); let file_max_log_level: level_filters::LevelFilter =
convert_loglevel(settingsr.logging.file.level)
.to_tracing_level()
.into();
let log_path = Path::new(&settingsr.logging.file.path); let log_path = Path::new(&settingsr.logging.file.path);
let full_path = std::env::current_dir() let full_path = std::env::current_dir()
@ -98,8 +89,9 @@ impl VeilidLogs {
fmt::Layer::new() fmt::Layer::new()
.compact() .compact()
.with_writer(non_blocking_appender) .with_writer(non_blocking_appender)
.with_filter(file_max_log_level)
.with_filter(filter::FilterFn::new(move |metadata| { .with_filter(filter::FilterFn::new(move |metadata| {
logfilter(metadata, file_max_log_level, &ignore_list) logfilter(metadata, &ignore_list)
})), })),
) )
} else { } else {
@ -117,10 +109,13 @@ impl VeilidLogs {
if #[cfg(target_os = "linux")] { if #[cfg(target_os = "linux")] {
let subscriber = subscriber.with(if settingsr.logging.system.enabled { let subscriber = subscriber.with(if settingsr.logging.system.enabled {
let ignore_list = ignore_list.clone(); let ignore_list = ignore_list.clone();
let system_max_log_level = convert_loglevel(settingsr.logging.system.level); let system_max_log_level: level_filters::LevelFilter = convert_loglevel(settingsr.logging.system.level).to_tracing_level().into();
Some(tracing_journald::layer().map_err(|e| format!("failed to set up journald logging: {}", e))?.with_filter(filter::FilterFn::new(move |metadata| { Some(tracing_journald::layer().map_err(|e| format!("failed to set up journald logging: {}", e))?
logfilter(metadata, system_max_log_level, &ignore_list) .with_filter(system_max_log_level)
}))) .with_filter(filter::FilterFn::new(move |metadata| {
logfilter(metadata, &ignore_list)
}))
)
} else { } else {
None None
}); });