veilid/veilid-server/src/veilid_logs.rs

132 lines
4.9 KiB
Rust
Raw Normal View History

2022-01-15 23:24:37 +00:00
use crate::settings::*;
2022-05-16 15:52:48 +00:00
use cfg_if::*;
2022-06-08 01:31:05 +00:00
use std::path::*;
use tracing::*;
use tracing_appender::*;
use tracing_subscriber::prelude::*;
use tracing_subscriber::*;
2022-01-15 23:24:37 +00:00
pub struct VeilidLogs {
2022-06-08 01:31:05 +00:00
pub guard: Option<non_blocking::WorkerGuard>,
2022-01-15 23:24:37 +00:00
}
2022-06-08 15:45:30 +00:00
fn logfilter<T: AsRef<str>, V: AsRef<[T]>>(metadata: &Metadata, ignore_list: V) -> bool {
2022-06-08 01:31:05 +00:00
// Skip filtered targets
!match (metadata.target(), ignore_list.as_ref()) {
(path, ignore) if !ignore.is_empty() => {
// Check that the module path does not match any ignore filters
ignore.iter().any(|v| path.starts_with(v.as_ref()))
2022-05-16 15:52:48 +00:00
}
2022-06-08 01:31:05 +00:00
_ => false,
2022-05-16 15:52:48 +00:00
}
}
2022-01-15 23:24:37 +00:00
impl VeilidLogs {
2022-06-08 01:31:05 +00:00
pub fn setup(settings: Settings) -> Result<VeilidLogs, String> {
2022-01-15 23:24:37 +00:00
let settingsr = settings.read();
2022-06-08 01:31:05 +00:00
// Set up subscriber and layers
let mut ignore_list = Vec::<String>::new();
2022-02-01 03:47:17 +00:00
for ig in veilid_core::DEFAULT_LOG_IGNORE_LIST {
2022-06-08 01:31:05 +00:00
ignore_list.push(ig.to_owned());
2022-02-01 03:47:17 +00:00
}
2022-01-15 23:24:37 +00:00
2022-06-08 01:31:05 +00:00
let subscriber = Registry::default();
let subscriber = subscriber.with(if settingsr.logging.terminal.enabled {
2022-06-08 15:45:30 +00:00
let terminal_max_log_level: level_filters::LevelFilter =
convert_loglevel(settingsr.logging.terminal.level)
.to_tracing_level()
.into();
2022-06-08 13:33:41 +00:00
let ignore_list = ignore_list.clone();
2022-06-08 01:31:05 +00:00
Some(
fmt::Layer::new()
.compact()
.with_writer(std::io::stdout)
2022-06-08 15:45:30 +00:00
.with_filter(terminal_max_log_level)
2022-06-08 01:31:05 +00:00
.with_filter(filter::FilterFn::new(move |metadata| {
2022-06-08 15:45:30 +00:00
logfilter(metadata, &ignore_list)
2022-06-08 01:31:05 +00:00
})),
)
} else {
None
});
let mut guard = None;
let subscriber = subscriber.with(if settingsr.logging.file.enabled {
2022-06-08 15:45:30 +00:00
let file_max_log_level: level_filters::LevelFilter =
convert_loglevel(settingsr.logging.file.level)
.to_tracing_level()
.into();
2022-01-15 23:24:37 +00:00
2022-06-08 01:31:05 +00:00
let log_path = Path::new(&settingsr.logging.file.path);
let full_path = std::env::current_dir()
.unwrap_or(PathBuf::from(MAIN_SEPARATOR.to_string()))
.join(log_path);
let log_parent = full_path
.parent()
.unwrap_or(Path::new(&MAIN_SEPARATOR.to_string()))
.canonicalize()
.map_err(|e| {
format!(
"File log path parent does not exist: {} ({})",
settingsr.logging.file.path, e
)
})?;
let log_filename = full_path.file_name().ok_or(format!(
"File log filename not specified in path: {}",
settingsr.logging.file.path
))?;
let appender = tracing_appender::rolling::never(log_parent, Path::new(log_filename));
let (non_blocking_appender, non_blocking_guard) =
tracing_appender::non_blocking(appender);
guard = Some(non_blocking_guard);
2022-06-08 13:33:41 +00:00
let ignore_list = ignore_list.clone();
2022-06-08 01:31:05 +00:00
Some(
fmt::Layer::new()
.compact()
.with_writer(non_blocking_appender)
2022-06-08 15:45:30 +00:00
.with_filter(file_max_log_level)
2022-06-08 13:33:41 +00:00
.with_filter(filter::FilterFn::new(move |metadata| {
2022-06-08 15:45:30 +00:00
logfilter(metadata, &ignore_list)
2022-06-08 01:31:05 +00:00
})),
)
} else {
None
});
let subscriber = subscriber.with(if settingsr.logging.api.enabled {
// Get layer from veilid core, filtering is done by ApiTracingLayer automatically
Some(veilid_core::ApiTracingLayer::get())
} else {
None
});
2022-01-15 23:24:37 +00:00
2022-05-16 15:52:48 +00:00
cfg_if! {
if #[cfg(target_os = "linux")] {
2022-06-08 01:31:05 +00:00
let subscriber = subscriber.with(if settingsr.logging.system.enabled {
2022-06-08 13:33:41 +00:00
let ignore_list = ignore_list.clone();
2022-06-08 15:45:30 +00:00
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(system_max_log_level)
.with_filter(filter::FilterFn::new(move |metadata| {
logfilter(metadata, &ignore_list)
}))
)
2022-06-08 01:31:05 +00:00
} else {
None
});
2022-05-16 15:52:48 +00:00
}
}
2022-06-08 01:31:05 +00:00
subscriber
.try_init()
.map_err(|e| format!("failed to initialize logging: {}", e))?;
2022-01-15 23:24:37 +00:00
2022-06-08 01:31:05 +00:00
Ok(VeilidLogs { guard })
2022-01-15 23:24:37 +00:00
}
}