diff --git a/pagetop/src/app.rs b/pagetop/src/app.rs index 843a32b5..45ebc351 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; #[cfg(feature = "database")] diff --git a/pagetop/src/app/application.rs b/pagetop/src/app/application.rs index 4b0e0ff9..28145164 100644 --- a/pagetop/src/app/application.rs +++ b/pagetop/src/app/application.rs @@ -20,7 +20,7 @@ impl Application { super::banner::print_on_startup(); // Inicia registro de trazas y eventos. - LazyStatic::force(&super::tracing::TRACING); + LazyStatic::force(&global::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 3c7b58a8..00000000 --- a/pagetop/src/app/tracing.rs +++ /dev/null @@ -1,64 +0,0 @@ -use crate::{global, 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(&global::SETTINGS.log.tracing).unwrap_or_else(|_| EnvFilter::new("Info")); - - let rolling = global::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 = &global::SETTINGS.log.path; - let prefix = &global::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.", - global::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 global::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.", - global::SETTINGS.log.format, - ); - subscriber.init(); - } - } - - guard -}); diff --git a/pagetop/src/config.rs b/pagetop/src/config.rs index a49b0a71..f70276d8 100644 --- a/pagetop/src/config.rs +++ b/pagetop/src/config.rs @@ -79,11 +79,11 @@ //! ``` //! //! De hecho, así se declaran los ajustes globales de la configuración (ver -//! [`SETTINGS`](crate::app::config::SETTINGS)). +//! [`SETTINGS`](crate::global::SETTINGS)). //! //! Puedes usar la [sintaxis TOML](https://toml.io/en/v1.0.0#table) para añadir tu nueva sección //! `[myapp]` en los archivos de configuración, del mismo modo que se añaden `[log]` o `[server]` en -//! los ajustes globales (ver [`Settings`](crate::app::config::Settings)). +//! los ajustes globales (ver [`Settings`](crate::global::Settings)). //! //! Se recomienda inicializar todos los ajustes con valores predefinidos, o utilizar la notación //! `Option` si van a ser tratados en el código como opcionales. diff --git a/pagetop/src/global.rs b/pagetop/src/global.rs index 041216db..ce687ff4 100644 --- a/pagetop/src/global.rs +++ b/pagetop/src/global.rs @@ -1,7 +1,12 @@ -use crate::pub_config; +use crate::{pub_config, LazyStatic}; use serde::Deserialize; +use tracing_appender::non_blocking::WorkerGuard; +use tracing_subscriber::EnvFilter; + +// CONFIGURACIÓN *********************************************************************************** + #[derive(Debug, Deserialize)] /// Ajustes globales para las secciones reservadas [`[app]`](App), [`[database]`](Database), /// [`[dev]`](Dev), [`[log]`](Log) y [`[server]`](Server) (ver [`SETTINGS`]). @@ -124,3 +129,63 @@ pub_config!(SETTINGS: Settings, "server.bind_address" => "localhost", "server.bind_port" => 8088, ); + +// REGISTRO DE TRAZAS Y EVENTOS ******************************************************************** + +/// 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(&SETTINGS.log.tracing).unwrap_or_else(|_| EnvFilter::new("Info")); + + let rolling = 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 = &SETTINGS.log.path; + let prefix = &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.", + 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 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.", + SETTINGS.log.format, + ); + subscriber.init(); + } + } + + guard +});