Add tracing-appender for improved logging with daily rotation.
This commit is contained in:
parent
9b51998889
commit
aebca7a7e2
5 changed files with 74 additions and 47 deletions
|
|
@ -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"] }
|
||||
|
||||
|
|
|
|||
|
|
@ -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).
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue