♻️ [global] Incorpora registro de trazas y eventos
This commit is contained in:
parent
aa9f392ba9
commit
c7bf549434
5 changed files with 69 additions and 70 deletions
|
|
@ -6,8 +6,6 @@ pub use actix_web_static_files::ResourceFiles;
|
||||||
|
|
||||||
mod banner;
|
mod banner;
|
||||||
|
|
||||||
mod tracing;
|
|
||||||
|
|
||||||
pub mod locale;
|
pub mod locale;
|
||||||
|
|
||||||
#[cfg(feature = "database")]
|
#[cfg(feature = "database")]
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ impl Application {
|
||||||
super::banner::print_on_startup();
|
super::banner::print_on_startup();
|
||||||
|
|
||||||
// Inicia registro de trazas y eventos.
|
// Inicia registro de trazas y eventos.
|
||||||
LazyStatic::force(&super::tracing::TRACING);
|
LazyStatic::force(&global::TRACING);
|
||||||
|
|
||||||
// Valida el identificador de idioma.
|
// Valida el identificador de idioma.
|
||||||
LazyStatic::force(&super::locale::LANGID);
|
LazyStatic::force(&super::locale::LANGID);
|
||||||
|
|
|
||||||
|
|
@ -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<WorkerGuard>` se garantiza que todos los registros almacenados se
|
|
||||||
/// enviarán antes de terminar la ejecución.
|
|
||||||
|
|
||||||
#[rustfmt::skip]
|
|
||||||
pub static TRACING: LazyStatic<WorkerGuard> = 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
|
|
||||||
});
|
|
||||||
|
|
@ -79,11 +79,11 @@
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! De hecho, así se declaran los ajustes globales de la configuración (ver
|
//! 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
|
//! 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
|
//! `[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
|
//! Se recomienda inicializar todos los ajustes con valores predefinidos, o utilizar la notación
|
||||||
//! `Option<T>` si van a ser tratados en el código como opcionales.
|
//! `Option<T>` si van a ser tratados en el código como opcionales.
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,12 @@
|
||||||
use crate::pub_config;
|
use crate::{pub_config, LazyStatic};
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
use tracing_appender::non_blocking::WorkerGuard;
|
||||||
|
use tracing_subscriber::EnvFilter;
|
||||||
|
|
||||||
|
// CONFIGURACIÓN ***********************************************************************************
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
/// Ajustes globales para las secciones reservadas [`[app]`](App), [`[database]`](Database),
|
/// Ajustes globales para las secciones reservadas [`[app]`](App), [`[database]`](Database),
|
||||||
/// [`[dev]`](Dev), [`[log]`](Log) y [`[server]`](Server) (ver [`SETTINGS`]).
|
/// [`[dev]`](Dev), [`[log]`](Log) y [`[server]`](Server) (ver [`SETTINGS`]).
|
||||||
|
|
@ -124,3 +129,63 @@ pub_config!(SETTINGS: Settings,
|
||||||
"server.bind_address" => "localhost",
|
"server.bind_address" => "localhost",
|
||||||
"server.bind_port" => 8088,
|
"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<WorkerGuard>` se garantiza que todos
|
||||||
|
/// los registros almacenados se enviarán antes de terminar la ejecución.
|
||||||
|
|
||||||
|
#[rustfmt::skip]
|
||||||
|
pub(crate) static TRACING: LazyStatic<WorkerGuard> = 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
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue