Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rpxy-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ serde_ignored = "0.1.12"
# logging
tracing = { version = "0.1.41" }
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
tracing-appender = "0.2.3"

################################
# cert management
Expand Down
32 changes: 13 additions & 19 deletions rpxy-bin/src/log.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::constants::{ACCESS_LOG_FILE, SYSTEM_LOG_FILE};
use rpxy_lib::log_event_names;
use std::str::FromStr;
use std::{str::FromStr, sync::OnceLock};
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::{filter::filter_fn, fmt, prelude::*};

#[allow(unused)]
Expand All @@ -19,6 +20,9 @@ pub fn init_logger(log_dir_path: Option<&str>) {
}
}

static ACCESS_LOG_GUARD: OnceLock<WorkerGuard> = OnceLock::new();
static SYSTEM_LOG_GUARD: OnceLock<WorkerGuard> = OnceLock::new();

/// file logging
fn init_file_logger(level: tracing::Level, log_dir_path: &str) {
println!("Activate logging to files: {}", log_dir_path);
Expand All @@ -35,8 +39,12 @@ fn init_file_logger(level: tracing::Level, log_dir_path: &str) {
println!("Access log: {}", access_log_path.display());
println!("System and error log: {}", system_log_path.display());

let access_log = open_log_file(&access_log_path);
let system_log = open_log_file(&system_log_path);
let access_log_appender = tracing_appender::rolling::daily(&log_dir_path, ACCESS_LOG_FILE);
let (access_non_blocking, guard) = tracing_appender::non_blocking(access_log_appender);
_ = ACCESS_LOG_GUARD.set(guard);
let system_log_appender = tracing_appender::rolling::daily(log_dir_path, SYSTEM_LOG_FILE);
let (system_non_blocking, guard) = tracing_appender::non_blocking(system_log_appender);
_ = SYSTEM_LOG_GUARD.set(guard);

let access_layer = fmt::layer()
.with_line_number(false)
Expand All @@ -46,7 +54,7 @@ fn init_file_logger(level: tracing::Level, log_dir_path: &str) {
.with_level(false)
.compact()
.with_ansi(false)
.with_writer(access_log)
.with_writer(access_non_blocking)
.with_filter(AccessLogFilter);

let system_layer = fmt::layer()
Expand All @@ -57,7 +65,7 @@ fn init_file_logger(level: tracing::Level, log_dir_path: &str) {
.with_level(true)
.compact()
.with_ansi(false)
.with_writer(system_log)
.with_writer(system_non_blocking)
.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)
Expand Down Expand Up @@ -104,20 +112,6 @@ impl<S> tracing_subscriber::layer::Filter<S> for AccessLogFilter {
}
}

#[inline]
/// Create a file for logging
fn open_log_file<P>(path: P) -> std::fs::File
where
P: AsRef<std::path::Path>,
{
// create a file if it does not exist
std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(path)
.expect("Failed to open the log file")
}

#[inline]
/// Matches cargo package name with `_` instead of `-`
fn is_cargo_pkg(metadata: &tracing::Metadata<'_>) -> bool {
Expand Down