Add tracing-appender for improved logging with daily rotation.

This commit is contained in:
Greg Burri 2025-04-12 17:27:03 +02:00
parent 9b51998889
commit aebca7a7e2
5 changed files with 74 additions and 47 deletions

View file

@ -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).

View file

@ -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<P>(directory: P)
pub fn init<P>(directory: P) -> WorkerGuard
where
P: AsRef<Path>,
{
@ -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
}

View file

@ -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);