From 7149980e5de043104b48b3c8a4bf158e6556f51c Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sat, 12 Nov 2022 13:04:20 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Simplifica=20estructura=20?= =?UTF-8?q?API=20de=20tracing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/src/app.rs | 2 -- pagetop/src/app/application.rs | 4 +-- pagetop/src/app/tracing.rs | 62 --------------------------------- pagetop/src/trace.rs | 63 ++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 66 deletions(-) delete mode 100644 pagetop/src/app/tracing.rs diff --git a/pagetop/src/app.rs b/pagetop/src/app.rs index 702a5a45..1c9218c3 100644 --- a/pagetop/src/app.rs +++ b/pagetop/src/app.rs @@ -6,8 +6,6 @@ pub use actix_web_static_files::ResourceFiles; mod banner; -mod tracing; - pub mod locale; pub mod application; diff --git a/pagetop/src/app/application.rs b/pagetop/src/app/application.rs index 7213ddae..c15e4971 100644 --- a/pagetop/src/app/application.rs +++ b/pagetop/src/app/application.rs @@ -4,7 +4,7 @@ use crate::core::module::ModuleStaticRef; use crate::core::{module, theme}; use crate::html::Markup; use crate::response::page::ResultPage; -use crate::{config, db, LazyStatic}; +use crate::{config, db, trace, LazyStatic}; use actix_web::dev::Server; @@ -20,7 +20,7 @@ impl Application { super::banner::print_on_startup(); // Inicia registro de trazas y eventos. - LazyStatic::force(&super::tracing::TRACING); + LazyStatic::force(&trace::TRACING); // Valida el identificador de idioma. LazyStatic::force(&super::locale::LANGID); diff --git a/pagetop/src/app/tracing.rs b/pagetop/src/app/tracing.rs deleted file mode 100644 index baac884d..00000000 --- a/pagetop/src/app/tracing.rs +++ /dev/null @@ -1,62 +0,0 @@ -use crate::{config, LazyStatic}; - -use tracing_appender::non_blocking::WorkerGuard; -use tracing_subscriber::EnvFilter; - -/// Registro de trazas y eventos de la aplicación. -/// -/// Para aumentar el rendimiento, un subproceso dedicado utiliza un sistema de escritura sin bloqueo -/// (*non-blocking writer*) que actúa periódicamente en vez de enviar cada traza o evento al -/// instante. Si el programa termina abruptamente (por ejemplo, por un panic! o un -/// std::process::exit), es posible que algunas trazas o eventos no se envíen. -/// -/// Puesto que las trazas o eventos registrados poco antes de la caída de una aplicación suelen ser -/// importantes para diagnosticar la causa del fallo, con `Lazy` se garantiza que todos -/// los registros almacenados se enviarán antes de terminar la ejecución. - -#[rustfmt::skip] -pub static TRACING: LazyStatic = LazyStatic::new(|| { - let env_filter = - EnvFilter::try_new(&config::SETTINGS.log.tracing).unwrap_or_else(|_| EnvFilter::new("Info")); - - let rolling = config::SETTINGS.log.rolling.to_lowercase(); - let (non_blocking, guard) = match rolling.as_str() { - "stdout" => tracing_appender::non_blocking(std::io::stdout()), - _ => tracing_appender::non_blocking({ - let path = &config::SETTINGS.log.path; - let prefix = &config::SETTINGS.log.prefix; - match rolling.as_str() { - "daily" => tracing_appender::rolling::daily(path, prefix), - "hourly" => tracing_appender::rolling::hourly(path, prefix), - "minutely" => tracing_appender::rolling::minutely(path, prefix), - "endless" => tracing_appender::rolling::never(path, prefix), - _ => { - println!( - "Rolling value \"{}\" not valid. Using \"daily\". Check the settings file.", - config::SETTINGS.log.rolling, - ); - tracing_appender::rolling::daily(path, prefix) - } - } - }), - }; - let subscriber = tracing_subscriber::fmt() - .with_env_filter(env_filter) - .with_writer(non_blocking) - .with_ansi(rolling.as_str() == "stdout"); - match config::SETTINGS.log.format.to_lowercase().as_str() { - "json" => subscriber.json().init(), - "full" => subscriber.init(), - "compact" => subscriber.compact().init(), - "pretty" => subscriber.pretty().init(), - _ => { - println!( - "Tracing format \"{}\" not valid. Using \"Full\". Check the settings file.", - config::SETTINGS.log.format, - ); - subscriber.init(); - } - } - - guard -}); diff --git a/pagetop/src/trace.rs b/pagetop/src/trace.rs index 2b2f3fb1..14e9bfe2 100644 --- a/pagetop/src/trace.rs +++ b/pagetop/src/trace.rs @@ -15,5 +15,68 @@ //! de un árbol anidado de intervalos similares. Además, estos intervalos están *estructurados*, con //! capacidad para grabar tipos de datos y mensajes de texto. +use crate::{config, LazyStatic}; + pub use tracing::{debug, error, info, trace, warn}; pub use tracing::{event, span, Level}; + +use tracing_appender::non_blocking::WorkerGuard; +use tracing_subscriber::EnvFilter; + +/// Registro de trazas y eventos de la aplicación. +/// +/// Para aumentar el rendimiento, un subproceso dedicado utiliza un sistema de escritura sin bloqueo +/// (*non-blocking writer*) que actúa periódicamente en vez de enviar cada traza o evento al +/// instante. Si el programa termina abruptamente (por ejemplo, por un panic! o un +/// std::process::exit), es posible que algunas trazas o eventos no se envíen. +/// +/// Puesto que las trazas o eventos registrados poco antes de la caída de una aplicación suelen ser +/// importantes para diagnosticar la causa del fallo, con `Lazy` se garantiza que todos +/// los registros almacenados se enviarán antes de terminar la ejecución. + +#[rustfmt::skip] +pub(crate) static TRACING: LazyStatic = LazyStatic::new(|| { + let env_filter = + EnvFilter::try_new(&config::SETTINGS.log.tracing).unwrap_or_else(|_| EnvFilter::new("Info")); + + let rolling = config::SETTINGS.log.rolling.to_lowercase(); + let (non_blocking, guard) = match rolling.as_str() { + "stdout" => tracing_appender::non_blocking(std::io::stdout()), + _ => tracing_appender::non_blocking({ + let path = &config::SETTINGS.log.path; + let prefix = &config::SETTINGS.log.prefix; + match rolling.as_str() { + "daily" => tracing_appender::rolling::daily(path, prefix), + "hourly" => tracing_appender::rolling::hourly(path, prefix), + "minutely" => tracing_appender::rolling::minutely(path, prefix), + "endless" => tracing_appender::rolling::never(path, prefix), + _ => { + println!( + "Rolling value \"{}\" not valid. Using \"daily\". Check the settings file.", + config::SETTINGS.log.rolling, + ); + tracing_appender::rolling::daily(path, prefix) + } + } + }), + }; + let subscriber = tracing_subscriber::fmt() + .with_env_filter(env_filter) + .with_writer(non_blocking) + .with_ansi(rolling.as_str() == "stdout"); + match config::SETTINGS.log.format.to_lowercase().as_str() { + "json" => subscriber.json().init(), + "full" => subscriber.init(), + "compact" => subscriber.compact().init(), + "pretty" => subscriber.pretty().init(), + _ => { + println!( + "Tracing format \"{}\" not valid. Using \"Full\". Check the settings file.", + config::SETTINGS.log.format, + ); + subscriber.init(); + } + } + + guard +});