From aebca7a7e248bb62cb1ea6b1fca4bcb9cf770682 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sat, 12 Apr 2025 17:27:03 +0200 Subject: [PATCH] Add tracing-appender for improved logging with daily rotation. --- Cargo.lock | 58 +++++++++++++++++++++++++++++-------------- backend/Cargo.toml | 1 + backend/src/consts.rs | 3 +++ backend/src/log.rs | 55 +++++++++++++++++++++------------------- backend/src/main.rs | 4 +-- 5 files changed, 74 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 315bc86..473f033 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -389,9 +389,9 @@ dependencies = [ [[package]] name = "bon" -version = "3.5.1" +version = "3.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65268237be94042665b92034f979c42d431d2fd998b49809543afe3e66abad1c" +checksum = "92c5f8abc69af414cbd6f2103bb668b91e584072f2105e4b38bed79b6ad0975f" dependencies = [ "bon-macros", "rustversion", @@ -399,9 +399,9 @@ dependencies = [ [[package]] name = "bon-macros" -version = "3.5.1" +version = "3.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "803c95b2ecf650eb10b5f87dda6b9f6a1b758cee53245e2b7b825c9b3803a443" +checksum = "b69edf39b6f321cb2699a93fc20c256adb839719c42676d03f7aa975e4e5581d" dependencies = [ "darling", "ident_case", @@ -441,9 +441,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.18" +version = "1.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "525046617d8376e3db1deffb079e91cef90a89fc3ca5c185bbf8c9ecdd15cd5c" +checksum = "8e3a13707ac958681c13b39b458c073d0d9bc8a22cb1b2f4c8e55eb72c13f362" dependencies = [ "shlex", ] @@ -482,9 +482,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.35" +version = "4.5.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8aa86934b44c19c50f87cc2790e19f54f7a67aedb64101c2e1a2e5ecfb73944" +checksum = "2df961d8c8a0d08aa9945718ccf584145eee3f3aa06cddbeac12933781102e04" dependencies = [ "clap_builder", "clap_derive", @@ -492,9 +492,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.35" +version = "4.5.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2414dbb2dd0695280da6ea9261e327479e9d37b0630f6b53ba2a11c60c679fd9" +checksum = "132dbda40fb6753878316a489d5a1242a8ef2f0d9e47ba01c951ea8aa7d013a5" dependencies = [ "anstream", "anstyle", @@ -630,6 +630,15 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-queue" version = "0.3.12" @@ -756,9 +765,9 @@ dependencies = [ [[package]] name = "email-encoding" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20b9cde6a71f9f758440470f3de16db6c09a02c443ce66850d87f5410548fb8e" +checksum = "9298e6504d9b9e780ed3f7dfd43a61be8cd0e09eb07f7706a945b0072b6670b6" dependencies = [ "base64", "memchr", @@ -1674,9 +1683,9 @@ checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" [[package]] name = "linux-raw-sys" -version = "0.9.3" +version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe7db12097d22ec582439daf8618b8fdd1a7bef6270e9af3b1ebcd30893cf413" +checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" [[package]] name = "litemap" @@ -1749,9 +1758,9 @@ dependencies = [ [[package]] name = "miniz_oxide" -version = "0.8.7" +version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff70ce3e48ae43fa075863cef62e8b43b71a4f2382229920e0df362592919430" +checksum = "3be647b768db090acb35d5ec5db2b0e1f1de11133ca123b9eacf5137868f892a" dependencies = [ "adler2", ] @@ -2196,6 +2205,7 @@ dependencies = [ "tower", "tower-http", "tracing", + "tracing-appender", "tracing-subscriber", ] @@ -2326,9 +2336,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.25" +version = "0.23.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "822ee9188ac4ec04a2f0531e55d035fb2de73f18b41a63c70c2712503b6fb13c" +checksum = "df51b5869f3a441595eac5e8ff14d486ff285f7b8c0df8770e49c3b56351f0f0" dependencies = [ "log", "once_cell", @@ -3166,6 +3176,18 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-appender" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3566e8ce28cc0a3fe42519fc80e6b4c943cc4c8cef275620eb8dac2d3d4e06cf" +dependencies = [ + "crossbeam-channel", + "thiserror 1.0.69", + "time", + "tracing-subscriber", +] + [[package]] name = "tracing-attributes" version = "0.1.28" diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 34f6573..6e3fb03 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -15,6 +15,7 @@ tower-http = { version = "0.6", features = ["fs", "trace"] } tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-appender = "0.2" chrono = { version = "0.4", features = ["serde"] } diff --git a/backend/src/consts.rs b/backend/src/consts.rs index 1e74425..f178c62 100644 --- a/backend/src/consts.rs +++ b/backend/src/consts.rs @@ -18,6 +18,9 @@ pub const DB_FILENAME: &str = "recipes.sqlite"; /// Path to the SQL file which contains the initial database schema. pub const SQL_FILENAME: &str = "sql/version_{VERSION}.sql"; +/// Base name for the log files, the date and the extension will be appended to it. +pub const BASE_LOG_FILENAME: &str = "recipes"; + /// When a new user sign up, he has this duration to validate his account. pub const VALIDATION_TOKEN_DURATION: i64 = 60 * 60; // [s]. (1 jour). diff --git a/backend/src/log.rs b/backend/src/log.rs index e941a13..45ab1b9 100644 --- a/backend/src/log.rs +++ b/backend/src/log.rs @@ -1,9 +1,15 @@ use std::{fs, path::Path}; +use tracing_appender::{ + non_blocking::WorkerGuard, + rolling::{RollingFileAppender, Rotation}, +}; use tracing_subscriber::{ fmt::writer::MakeWriterExt, layer::SubscriberExt, util::SubscriberInitExt, }; +use crate::consts; + #[cfg(debug_assertions)] const TRACING_LEVEL: tracing::Level = tracing::Level::DEBUG; @@ -16,7 +22,7 @@ const TRACING_LEVEL: tracing::Level = tracing::Level::INFO; #[cfg(not(debug_assertions))] const TRACING_DISPLAY_THREAD: bool = false; -pub fn init

(directory: P) +pub fn init

(directory: P) -> WorkerGuard where P: AsRef, { @@ -24,33 +30,30 @@ where fs::DirBuilder::new().create(&directory).unwrap(); } - let log_filepath = directory.as_ref().join(format!( - "recipes_{}.log", - chrono::Local::now().format("%Y-%m-%d_%H%M%S") - )); + let file_appender = RollingFileAppender::builder() + .rotation(Rotation::DAILY) + .filename_prefix(consts::BASE_LOG_FILENAME) + .filename_suffix("log") + .build(directory) + .expect("Initializing rolling file appender failed"); - println!("log file: {}", log_filepath.to_str().unwrap_or_default()); + let (non_blocking, guard) = tracing_appender::non_blocking(file_appender); - match std::fs::File::create(log_filepath) { - Ok(log_file) => { - let layer_file = tracing_subscriber::fmt::layer() - .with_writer(log_file.with_max_level(TRACING_LEVEL)) - .with_ansi(false) - .with_thread_ids(TRACING_DISPLAY_THREAD) - .with_thread_names(TRACING_DISPLAY_THREAD); + let layer_file = tracing_subscriber::fmt::layer() + .with_writer(non_blocking.with_max_level(TRACING_LEVEL)) + .with_ansi(false) + .with_thread_ids(TRACING_DISPLAY_THREAD) + .with_thread_names(TRACING_DISPLAY_THREAD); - let layer_stdout = tracing_subscriber::fmt::layer() - .with_writer(std::io::stdout.with_max_level(TRACING_LEVEL)) - .with_thread_ids(TRACING_DISPLAY_THREAD) - .with_thread_names(TRACING_DISPLAY_THREAD); + let layer_stdout = tracing_subscriber::fmt::layer() + .with_writer(std::io::stdout.with_max_level(TRACING_LEVEL)) + .with_thread_ids(TRACING_DISPLAY_THREAD) + .with_thread_names(TRACING_DISPLAY_THREAD); - tracing_subscriber::Registry::default() - .with(layer_file) - .with(layer_stdout) - .init(); - } - Err(error) => { - println!("Unable to open log file: {}", error); - } - } + tracing_subscriber::Registry::default() + .with(layer_file) + .with(layer_stdout) + .init(); + + guard } diff --git a/backend/src/main.rs b/backend/src/main.rs index 41687ef..6742aa9 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -89,13 +89,11 @@ struct Context { dark_theme: bool, } -use tracing_subscriber::{Registry, fmt::layer, layer::SubscriberExt, util::SubscriberInitExt}; - // TODO: Should main returns 'Result'? #[tokio::main] async fn main() { let config = config::load(); - log::init(&config.logs_directory); + let _guard = log::init(&config.logs_directory); event!(Level::INFO, "Configuration: {:?}", config);