diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9d21ece0a..a4919f89d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -170,8 +170,7 @@ jobs: shell: pwsh run: | rustup target add aarch64-pc-windows-msvc - @('', '[patch.crates-io]', - 'ring = { git = "https://github.com/awakecoding/ring", branch = "0.16.20_alpha" }') | % { + 'ring = { git = "https://github.com/awakecoding/ring", branch = "0.16.20_alpha" }' | % { Add-Content -Path "./Cargo.toml" -Value $_ } $VSINSTALLDIR = $(vswhere.exe -latest -requires Microsoft.VisualStudio.Component.VC.Llvm.Clang -property installationPath) diff --git a/Cargo.lock b/Cargo.lock index 2a0662f15..78c43987f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -714,9 +714,9 @@ dependencies = [ [[package]] name = "crossbeam-channel" -version = "0.5.2" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e54ea8bc3fb1ee042f5aace6e3c6e025d3874866da222930f70ce62aceba0bfa" +checksum = "cf2b3e8478797446514c91ef04bafcb59faba183e621ad488df88983cc14128c" dependencies = [ "cfg-if 1.0.0", "crossbeam-utils", @@ -4641,11 +4641,11 @@ dependencies = [ [[package]] name = "tracing-appender" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09d48f71a791638519505cefafe162606f706c25592e4bde4d97600c0195312e" +version = "0.2.999" +source = "git+https://github.com/CBenoit/tracing?rev=454313f66da3a662#454313f66da3a66261955924bc283af147758550" dependencies = [ "crossbeam-channel", + "thiserror", "time 0.3.19", "tracing-subscriber", ] diff --git a/Cargo.toml b/Cargo.toml index 0d9375048..33fe42746 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,6 @@ default-members = [ [profile.production] inherits = "release" lto = true + +[patch.crates-io] +tracing-appender = { git = "https://github.com/CBenoit/tracing", rev = "454313f66da3a662" } diff --git a/devolutions-gateway/src/config.rs b/devolutions-gateway/src/config.rs index 652aaaa1e..17aec5714 100644 --- a/devolutions-gateway/src/config.rs +++ b/devolutions-gateway/src/config.rs @@ -152,7 +152,7 @@ impl Conf { let log_file = conf_file .log_file .clone() - .unwrap_or_else(|| Utf8PathBuf::from("gateway.log")) + .unwrap_or_else(|| Utf8PathBuf::from("gateway")) .pipe_ref(|path| normalize_data_path(path, &data_dir)); let jrl_file = conf_file diff --git a/devolutions-gateway/src/http/middlewares/log.rs b/devolutions-gateway/src/http/middlewares/log.rs index aaa7dc4a0..0288ca726 100644 --- a/devolutions-gateway/src/http/middlewares/log.rs +++ b/devolutions-gateway/src/http/middlewares/log.rs @@ -25,6 +25,13 @@ impl LogMiddleware { uri }; + let span = if uri.len() > 512 { + // Truncate long URI to keep log readable and prevent fast growing log file + info_span!("request", request_id = %operation_id, %method, uri = %&uri[..512]) + } else { + info_span!("request", request_id = %operation_id, %method, %uri) + }; + async move { let start_time = Instant::now(); @@ -42,7 +49,7 @@ impl LogMiddleware { Ok(ctx) } - .instrument(info_span!("request", request_id = %operation_id, %method, %uri)) + .instrument(span) .await } } diff --git a/devolutions-gateway/src/log.rs b/devolutions-gateway/src/log.rs index b8e1338e1..262b07b54 100644 --- a/devolutions-gateway/src/log.rs +++ b/devolutions-gateway/src/log.rs @@ -7,10 +7,14 @@ use tokio::fs; use tokio::time::{sleep, Duration}; use tracing::metadata::LevelFilter; use tracing_appender::non_blocking::WorkerGuard; +use tracing_appender::rolling; use tracing_subscriber::filter::Directive; use tracing_subscriber::prelude::*; use tracing_subscriber::{fmt, EnvFilter}; +const MAX_BYTES_PER_LOG_FILE: u64 = 3_000_000; // 3 MB +const MAX_LOG_FILES: usize = 10; + pub struct LoggerGuard { _file_guard: WorkerGuard, _stdio_guard: WorkerGuard, @@ -26,7 +30,7 @@ impl<'a> LogPathCfg<'a> { if path.is_dir() { Ok(Self { folder: path, - prefix: "gateway.log", + prefix: "gateway", }) } else { Ok(Self { @@ -38,15 +42,16 @@ impl<'a> LogPathCfg<'a> { } pub fn init(path: &Utf8Path, filtering_directive: Option<&str>) -> anyhow::Result { - let (file_layer, file_guard) = { - let cfg = LogPathCfg::from_path(path)?; - - let file_appender = tracing_appender::rolling::daily(cfg.folder, cfg.prefix); - let (non_blocking, guard) = tracing_appender::non_blocking(file_appender); - let file_layer = fmt::layer().with_writer(non_blocking).with_ansi(false); - - (file_layer, guard) - }; + let log_cfg = LogPathCfg::from_path(path)?; + let file_appender = rolling::Builder::new() + .rotation(rolling::Rotation::max_bytes(MAX_BYTES_PER_LOG_FILE)) + .filename_prefix(log_cfg.prefix) + .filename_suffix("log") + .max_log_files(MAX_LOG_FILES) + .build(log_cfg.folder) + .context("Couldn’t create file appender")?; + let (file_non_blocking, file_guard) = tracing_appender::non_blocking(file_appender); + let file_layer = fmt::layer().with_writer(file_non_blocking).with_ansi(false); let (non_blocking_stdio, stdio_guard) = tracing_appender::non_blocking(std::io::stdout()); let stdio_layer = fmt::layer().with_writer(non_blocking_stdio); @@ -83,7 +88,7 @@ pub fn init(path: &Utf8Path, filtering_directive: Option<&str>) -> anyhow::Resul #[instrument] pub async fn log_deleter_task(prefix: &Utf8Path) -> anyhow::Result<()> { const TASK_INTERVAL: Duration = Duration::from_secs(60 * 60 * 24); // once per day - const MAX_AGE: Duration = Duration::from_secs(60 * 60 * 24 * 10); // 10 days + const MAX_AGE: Duration = Duration::from_secs(60 * 60 * 24 * 90); // 90 days debug!("Task started"); @@ -94,7 +99,7 @@ pub async fn log_deleter_task(prefix: &Utf8Path) -> anyhow::Result<()> { Ok(mut read_dir) => { while let Ok(Some(entry)) = read_dir.next_entry().await { match entry.file_name().to_str() { - Some(file_name) if file_name.starts_with(cfg.prefix) => { + Some(file_name) if file_name.starts_with(cfg.prefix) && file_name.contains("log") => { debug!(file_name, "Found a log file"); match entry .metadata() @@ -143,7 +148,7 @@ pub async fn find_latest_log_file(prefix: &Utf8Path) -> anyhow::Result { + Some(file_name) if file_name.starts_with(cfg.prefix) && file_name.contains("log") => { debug!(file_name, "Found a log file"); match entry.metadata().await.and_then(|metadata| metadata.modified()) { Ok(modified) if modified > most_recent_time => {