generated from PlexSheep/baserepo
refactor(log): refactor the build function
cargo devel CI / cargo CI (push) Has been cancelled
Details
cargo devel CI / cargo CI (push) Has been cancelled
Details
This commit is contained in:
parent
3063f77798
commit
6197532bc7
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
name = "libpt-log"
|
name = "libpt-log"
|
||||||
publish.workspace = true
|
publish.workspace = true
|
||||||
version = "0.6.1"
|
version = "0.6.2-alpha.0"
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
license.workspace = true
|
license.workspace = true
|
||||||
|
@ -13,12 +13,13 @@ keywords.workspace = true
|
||||||
categories.workspace = true
|
categories.workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tracing = "0.1.37"
|
tracing = "0.1.40"
|
||||||
tracing-appender = "0.2.2"
|
tracing-appender = "0.2.3"
|
||||||
tracing-subscriber = "0.3.17"
|
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
|
||||||
anyhow = { workspace = true }
|
anyhow = { workspace = true }
|
||||||
thiserror = { workspace = true }
|
thiserror = { workspace = true }
|
||||||
libpt-core = { workspace = true, optional = false }
|
libpt-core = { workspace = true, optional = false }
|
||||||
|
chrono = "0.4.38"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
gag = "1.0.0"
|
gag = "1.0.0"
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
#![warn(clippy::pedantic, clippy::style, clippy::nursery)]
|
#![warn(clippy::pedantic, clippy::style, clippy::nursery)]
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
fmt,
|
fmt::{self, Debug},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
sync::atomic::{AtomicBool, Ordering},
|
sync::atomic::{AtomicBool, Ordering},
|
||||||
};
|
};
|
||||||
|
@ -31,8 +31,9 @@ use error::Error;
|
||||||
/// I'm just repackaging it a little to make it more ergonomic
|
/// I'm just repackaging it a little to make it more ergonomic
|
||||||
pub use tracing;
|
pub use tracing;
|
||||||
pub use tracing::{debug, error, info, trace, warn, Level};
|
pub use tracing::{debug, error, info, trace, warn, Level};
|
||||||
use tracing_appender::{self};
|
use tracing_subscriber::{
|
||||||
use tracing_subscriber::fmt::{format::FmtSpan, time};
|
fmt::format::FmtSpan, layer::SubscriberExt as _, util::SubscriberInitExt, Layer,
|
||||||
|
};
|
||||||
|
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
/// The log level used when none is specified
|
/// The log level used when none is specified
|
||||||
|
@ -130,100 +131,59 @@ impl LoggerBuilder {
|
||||||
/// This function will return an error if a global Logger was aready initialized. This module
|
/// This function will return an error if a global Logger was aready initialized. This module
|
||||||
/// uses the [tracing] crate for logging, so if a [tracing] logger is initialized elsewhere,
|
/// uses the [tracing] crate for logging, so if a [tracing] logger is initialized elsewhere,
|
||||||
/// this method will error.
|
/// this method will error.
|
||||||
|
#[allow(clippy::missing_panics_doc)]
|
||||||
pub fn build(self) -> Result<Logger> {
|
pub fn build(self) -> Result<Logger> {
|
||||||
// only init if no init has been performed yet
|
// only init if no init has been performed yet
|
||||||
if INITIALIZED.load(Ordering::Relaxed) {
|
if INITIALIZED.load(Ordering::Relaxed) {
|
||||||
warn!("trying to reinitialize the logger, ignoring");
|
warn!("trying to reinitialize the logger, ignoring");
|
||||||
bail!(Error::Usage("logging is already initialized".to_string()));
|
bail!(Error::Usage("logging is already initialized".to_string()));
|
||||||
}
|
}
|
||||||
let subscriber = tracing_subscriber::fmt::Subscriber::builder()
|
let layer = tracing_subscriber::fmt::layer()
|
||||||
.with_level(self.display_level)
|
|
||||||
.with_max_level(self.max_level)
|
|
||||||
.with_ansi(self.ansi)
|
.with_ansi(self.ansi)
|
||||||
.with_target(self.display_target)
|
.with_target(self.display_target)
|
||||||
.with_file(self.display_filename)
|
.with_file(self.display_filename)
|
||||||
.with_thread_ids(self.display_thread_ids)
|
.with_thread_ids(self.display_thread_ids)
|
||||||
.with_line_number(self.display_line_number)
|
.with_line_number(self.display_line_number)
|
||||||
.with_thread_names(self.display_thread_names)
|
.with_thread_names(self.display_thread_names)
|
||||||
.with_span_events(self.span_events);
|
.with_span_events(self.span_events.clone())
|
||||||
// HACK: somehow find a better solution for this
|
.with_filter(tracing::level_filters::LevelFilter::from_level(
|
||||||
// I know this is hacky, but I couldn't get it any other way. I couldn't even find a
|
self.max_level,
|
||||||
// project that could do it any other way. You can't apply one after another, because the
|
));
|
||||||
// type is changed every time. When using `Box<dyn Whatever>`, some methods complain about
|
if self.log_to_file {
|
||||||
// not being in trait bounds.
|
tracing_subscriber::registry()
|
||||||
match (self.log_to_file, self.show_time, self.pretty, self.uptime) {
|
.with(layer.and_then(
|
||||||
(true, true, true, true) => {
|
tracing_subscriber::fmt::layer().with_writer(self.logfile().unwrap()),
|
||||||
let subscriber = subscriber
|
))
|
||||||
.with_writer(new_file_appender(self.log_dir))
|
.init();
|
||||||
.with_timer(time::uptime())
|
} else {
|
||||||
.pretty()
|
tracing_subscriber::registry().with(layer).init();
|
||||||
.finish();
|
|
||||||
tracing::subscriber::set_global_default(subscriber)?;
|
|
||||||
}
|
|
||||||
(true, true, true, false) => {
|
|
||||||
let subscriber = subscriber
|
|
||||||
.with_writer(new_file_appender(self.log_dir))
|
|
||||||
.pretty()
|
|
||||||
.finish();
|
|
||||||
tracing::subscriber::set_global_default(subscriber)?;
|
|
||||||
}
|
|
||||||
(true, false, true, _) => {
|
|
||||||
let subscriber = subscriber
|
|
||||||
.with_writer(new_file_appender(self.log_dir))
|
|
||||||
.without_time()
|
|
||||||
.pretty()
|
|
||||||
.finish();
|
|
||||||
tracing::subscriber::set_global_default(subscriber)?;
|
|
||||||
}
|
|
||||||
(true, true, false, true) => {
|
|
||||||
let subscriber = subscriber
|
|
||||||
.with_writer(new_file_appender(self.log_dir))
|
|
||||||
.with_timer(time::uptime())
|
|
||||||
.finish();
|
|
||||||
tracing::subscriber::set_global_default(subscriber)?;
|
|
||||||
}
|
|
||||||
(true, true, false, false) => {
|
|
||||||
let subscriber = subscriber
|
|
||||||
.with_writer(new_file_appender(self.log_dir))
|
|
||||||
.finish();
|
|
||||||
tracing::subscriber::set_global_default(subscriber)?;
|
|
||||||
}
|
|
||||||
(true, false, false, _) => {
|
|
||||||
let subscriber = subscriber
|
|
||||||
.with_writer(new_file_appender(self.log_dir))
|
|
||||||
.without_time()
|
|
||||||
.finish();
|
|
||||||
tracing::subscriber::set_global_default(subscriber)?;
|
|
||||||
}
|
|
||||||
(false, true, true, true) => {
|
|
||||||
let subscriber = subscriber.pretty().with_timer(time::uptime()).finish();
|
|
||||||
tracing::subscriber::set_global_default(subscriber)?;
|
|
||||||
}
|
|
||||||
(false, true, true, false) => {
|
|
||||||
let subscriber = subscriber.pretty().with_timer(time::uptime()).finish();
|
|
||||||
tracing::subscriber::set_global_default(subscriber)?;
|
|
||||||
}
|
|
||||||
(false, false, true, _) => {
|
|
||||||
let subscriber = subscriber.without_time().pretty().finish();
|
|
||||||
tracing::subscriber::set_global_default(subscriber)?;
|
|
||||||
}
|
|
||||||
(false, true, false, true) => {
|
|
||||||
let subscriber = subscriber.with_timer(time::uptime()).finish();
|
|
||||||
tracing::subscriber::set_global_default(subscriber)?;
|
|
||||||
}
|
|
||||||
(false, true, false, false) => {
|
|
||||||
let subscriber = subscriber.finish();
|
|
||||||
tracing::subscriber::set_global_default(subscriber)?;
|
|
||||||
}
|
|
||||||
(false, false, false, _) => {
|
|
||||||
let subscriber = subscriber.without_time().finish();
|
|
||||||
tracing::subscriber::set_global_default(subscriber)?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
INITIALIZED.store(true, Ordering::Relaxed);
|
INITIALIZED.store(true, Ordering::Relaxed);
|
||||||
Ok(Logger {})
|
Ok(Logger {})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn logfile(&self) -> Option<std::fs::File> {
|
||||||
|
if !self.log_to_file {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let mut path = self.log_dir.clone();
|
||||||
|
path.push(format!(
|
||||||
|
"{}.{}.log",
|
||||||
|
libpt_core::get_crate_name().unwrap_or_else(|| "logfile".to_string()),
|
||||||
|
chrono::Local::now().date_naive()
|
||||||
|
));
|
||||||
|
let file = match std::fs::File::create(path) {
|
||||||
|
Ok(f) => f,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("libpt: could not start logging to file: {e}");
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(file)
|
||||||
|
}
|
||||||
|
|
||||||
/// enable or disable logging to and creating of logfiles
|
/// enable or disable logging to and creating of logfiles
|
||||||
///
|
///
|
||||||
/// If you want to log to a file, don't forget to set [`Self::log_dir`]!
|
/// If you want to log to a file, don't forget to set [`Self::log_dir`]!
|
||||||
|
@ -490,13 +450,3 @@ impl Default for Logger {
|
||||||
.expect("building a Logger failed")
|
.expect("building a Logger failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_file_appender(log_dir: PathBuf) -> tracing_appender::rolling::RollingFileAppender {
|
|
||||||
tracing_appender::rolling::daily(
|
|
||||||
log_dir,
|
|
||||||
format!(
|
|
||||||
"{}.log",
|
|
||||||
libpt_core::get_crate_name().unwrap_or_else(|| "logfile".to_string())
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
Reference in New Issue