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