This commit is contained in:
Jun Kurihara 2025-05-19 15:21:07 +09:00
commit 4905d6a662
No known key found for this signature in database
GPG key ID: D992B3E3DE1DED23

View file

@ -1,7 +1,7 @@
use crate::constants::{ACCESS_LOG_FILE, SYSTEM_LOG_FILE}; use crate::constants::{ACCESS_LOG_FILE, SYSTEM_LOG_FILE};
use rpxy_lib::log_event_names; use rpxy_lib::log_event_names;
use std::str::FromStr; use std::str::FromStr;
use tracing_subscriber::{fmt, prelude::*}; use tracing_subscriber::{filter::filter_fn, fmt, prelude::*};
#[allow(unused)] #[allow(unused)]
pub use tracing::{debug, error, info, warn}; pub use tracing::{debug, error, info, warn};
@ -12,20 +12,16 @@ pub fn init_logger(log_dir_path: Option<&str>) {
let level = tracing::Level::from_str(level_string.as_str()).unwrap_or(tracing::Level::INFO); let level = tracing::Level::from_str(level_string.as_str()).unwrap_or(tracing::Level::INFO);
match log_dir_path { match log_dir_path {
None => { // log to stdout
// log to stdout None => init_stdio_logger(level),
init_stdio_logger(level); // log to files
} Some(log_dir_path) => init_file_logger(level, log_dir_path),
Some(log_dir_path) => {
// log to files
println!("Activate logging to files: {log_dir_path}");
init_file_logger(level, log_dir_path);
}
} }
} }
/// file logging TODO: /// file logging
fn init_file_logger(level: tracing::Level, log_dir_path: &str) { fn init_file_logger(level: tracing::Level, log_dir_path: &str) {
println!("Activate logging to files: {log_dir_path}");
let log_dir_path = std::path::PathBuf::from(log_dir_path); let log_dir_path = std::path::PathBuf::from(log_dir_path);
// create the directory if it does not exist // create the directory if it does not exist
if !log_dir_path.exists() { if !log_dir_path.exists() {
@ -63,12 +59,8 @@ fn init_file_logger(level: tracing::Level, log_dir_path: &str) {
let reg = reg.with( let reg = reg.with(
system_log_base system_log_base
.with_writer(system_log) .with_writer(system_log)
.with_filter(tracing_subscriber::filter::filter_fn(move |metadata| { .with_filter(filter_fn(move |metadata| {
(metadata (is_cargo_pkg(metadata) && metadata.name() != log_event_names::ACCESS_LOG && metadata.level() <= &level)
.target()
.starts_with(env!("CARGO_PKG_NAME").replace('-', "_").as_str())
&& metadata.name() != log_event_names::ACCESS_LOG
&& metadata.level() <= &level)
|| metadata.level() <= &tracing::Level::WARN.min(level) || metadata.level() <= &tracing::Level::WARN.min(level)
})), })),
); );
@ -85,12 +77,8 @@ fn init_stdio_logger(level: tracing::Level) {
let stdio_layer = stdio_layer let stdio_layer = stdio_layer
.with_target(false) .with_target(false)
.compact() .compact()
.with_filter(tracing_subscriber::filter::filter_fn(move |metadata| { .with_filter(filter_fn(move |metadata| {
(metadata (is_cargo_pkg(metadata) && metadata.level() <= &level) || metadata.level() <= &tracing::Level::WARN.min(level)
.target()
.starts_with(env!("CARGO_PKG_NAME").replace('-', "_").as_str())
&& metadata.level() <= &level)
|| metadata.level() <= &tracing::Level::WARN.min(level)
})); }));
tracing_subscriber::registry().with(stdio_layer).init(); tracing_subscriber::registry().with(stdio_layer).init();
} else { } else {
@ -101,12 +89,8 @@ fn init_stdio_logger(level: tracing::Level) {
.with_thread_names(true) .with_thread_names(true)
.with_target(true) .with_target(true)
.compact() .compact()
.with_filter(tracing_subscriber::filter::filter_fn(move |metadata| { .with_filter(filter_fn(move |metadata| {
(metadata (is_cargo_pkg(metadata) && metadata.level() <= &level) || metadata.level() <= &tracing::Level::INFO.min(level)
.target()
.starts_with(env!("CARGO_PKG_NAME").replace('-', "_").as_str())
&& metadata.level() <= &level)
|| metadata.level() <= &tracing::Level::INFO.min(level)
})); }));
tracing_subscriber::registry().with(stdio_layer).init(); tracing_subscriber::registry().with(stdio_layer).init();
}; };
@ -116,11 +100,7 @@ fn init_stdio_logger(level: tracing::Level) {
struct AccessLogFilter; struct AccessLogFilter;
impl<S> tracing_subscriber::layer::Filter<S> for AccessLogFilter { impl<S> tracing_subscriber::layer::Filter<S> for AccessLogFilter {
fn enabled(&self, metadata: &tracing::Metadata<'_>, _: &tracing_subscriber::layer::Context<'_, S>) -> bool { fn enabled(&self, metadata: &tracing::Metadata<'_>, _: &tracing_subscriber::layer::Context<'_, S>) -> bool {
metadata is_cargo_pkg(metadata) && metadata.name().contains(log_event_names::ACCESS_LOG) && metadata.level() <= &tracing::Level::INFO
.target()
.starts_with(env!("CARGO_PKG_NAME").replace('-', "_").as_str())
&& metadata.name().contains(log_event_names::ACCESS_LOG)
&& metadata.level() <= &tracing::Level::INFO
} }
} }
@ -137,3 +117,11 @@ where
.open(path) .open(path)
.expect("Failed to open the log file") .expect("Failed to open the log file")
} }
#[inline]
/// Mached with cargo package name with `_` instead of `-`
fn is_cargo_pkg(metadata: &tracing::Metadata<'_>) -> bool {
metadata
.target()
.starts_with(env!("CARGO_PKG_NAME").replace('-', "_").as_str())
}