fix(log): logfile works again #90

This commit is contained in:
Christoph J. Scherr 2024-07-09 19:37:56 +02:00
parent 1c92f95901
commit 729c4e3a4e
1 changed files with 26 additions and 15 deletions

View File

@ -6,8 +6,10 @@
//! For the library version, only the basic [`tracing`] is used, so that it is possible for //! For the library version, only the basic [`tracing`] is used, so that it is possible for
//! the end user to use the [`tracing`] frontend they desire. //! the end user to use the [`tracing`] frontend they desire.
//! //!
//! I did however decide to create a [`Logger`] struct. This struct is mainly intended to be used //! I did decide to create a [`Logger`] struct. This struct is mainly intended to be used with the
//! with the python module of [`pt`](../libpt/index.html), but is still just as usable in other contexts. //! python module of [`pt`](../libpt/index.html), but is still just as usable in other contexts.
//! You can use this struct when use of the macros is not possible, but the macros should generally
//! be preferred.
//! //!
//! ## Technologies used for logging: //! ## Technologies used for logging:
//! - [`tracing`]: base logging crate //! - [`tracing`]: base logging crate
@ -24,6 +26,9 @@ use std::{
pub mod error; pub mod error;
use error::Error; use error::Error;
/// This is the magic dependency where the cool stuff happens
///
/// 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, non_blocking::NonBlocking}; use tracing_appender::{self, non_blocking::NonBlocking};
@ -182,9 +187,10 @@ impl LoggerBuilder {
tracing::subscriber::set_global_default(subscriber)?; tracing::subscriber::set_global_default(subscriber)?;
} }
(true, false, false, _) => { (true, false, false, _) => {
let file_appender = tracing_appender::rolling::daily(self.log_dir, "log"); let subscriber = subscriber
let (file_writer, _guard) = tracing_appender::non_blocking(file_appender); .with_writer(new_file_appender(self.log_dir))
let subscriber = subscriber.with_writer(file_writer).without_time().finish(); .without_time()
.finish();
tracing::subscriber::set_global_default(subscriber)?; tracing::subscriber::set_global_default(subscriber)?;
} }
(false, true, true, true) => { (false, true, true, true) => {
@ -218,6 +224,8 @@ impl LoggerBuilder {
/// 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`]!
///
/// Default: false /// Default: false
#[must_use] #[must_use]
pub const fn log_to_file(mut self, log_to_file: bool) -> Self { pub const fn log_to_file(mut self, log_to_file: bool) -> Self {
@ -355,19 +363,23 @@ impl Default for LoggerBuilder {
/// ///
/// ## Levels /// ## Levels
/// ///
/// TODO: add levels desc and ascii art /// * [ERROR](Level::ERROR) Something broke
/// * [WARN](Level::WARN) Something is bad
/// * [INFO](Level::INFO) Useful information for users
/// * [DEBUG](Level::DEBUG) Useful information for developers
/// * [TRACE](Level::TRACE) Very verbose information for developers (often for libraries)
/// ///
/// ## Usage /// ## Usage
/// ///
/// You don't need to use the [Logger] struct, it's better to use the macros instead: /// You don't need to use the [Logger] struct, it's better to use the macros instead:
/// ///
/// * `error!` /// * [`error!`]
/// * `warn!` /// * [`warn!`]
/// * `info!` /// * [`info!`]
/// * `debug!` /// * [`debug!`]
/// * `trace!` /// * [`trace!`]
/// ///
/// You can however use the [Logger] struct in cases where usage of a macro is bad or /// You can however use the [Logger] struct in cases where usage of a macro is impossible or
/// you are somehow working with multiple loggers. The macros offer additional functionalities, /// you are somehow working with multiple loggers. The macros offer additional functionalities,
/// suck as full `format!` support and context, see [`tracing`], which we use as backend. /// suck as full `format!` support and context, see [`tracing`], which we use as backend.
/// ///
@ -449,7 +461,6 @@ impl Default for Logger {
} }
} }
fn new_file_appender(log_dir: PathBuf) -> NonBlocking { fn new_file_appender(log_dir: PathBuf) -> tracing_appender::rolling::RollingFileAppender {
let file_appender = tracing_appender::rolling::daily(log_dir, "log"); tracing_appender::rolling::daily(log_dir, format!("{}.log", env!("CARGO_CRATE_NAME")))
tracing_appender::non_blocking(file_appender).0
} }