Compare commits

..

No commits in common. "8226d74fb91de41deffceab0bdc207fb6ecc5dca" and "673eb691e99629543f3b196908a5785b095d6e68" have entirely different histories.

2 changed files with 15 additions and 46 deletions

View file

@ -1,11 +0,0 @@
use libpt_log::Logger;
use tracing::info;
fn main() {
let _logger = Logger::builder()
.log_to_file(true)
.log_dir("/tmp/llll".into())
.build()
.unwrap();
info!("foo bar qux");
}

View file

@ -6,10 +6,8 @@
//! 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 decide to create a [`Logger`] struct. This struct is mainly intended to be used with the //! I did however decide to create a [`Logger`] struct. This struct is mainly intended to be used
//! python module of [`pt`](../libpt/index.html), but is still just as usable in other contexts. //! with the 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
@ -26,9 +24,6 @@ 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};
@ -187,10 +182,9 @@ impl LoggerBuilder {
tracing::subscriber::set_global_default(subscriber)?; tracing::subscriber::set_global_default(subscriber)?;
} }
(true, false, false, _) => { (true, false, false, _) => {
let subscriber = subscriber let file_appender = tracing_appender::rolling::daily(self.log_dir, "log");
.with_writer(new_file_appender(self.log_dir)) let (file_writer, _guard) = tracing_appender::non_blocking(file_appender);
.without_time() let subscriber = subscriber.with_writer(file_writer).without_time().finish();
.finish();
tracing::subscriber::set_global_default(subscriber)?; tracing::subscriber::set_global_default(subscriber)?;
} }
(false, true, true, true) => { (false, true, true, true) => {
@ -224,8 +218,6 @@ 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 {
@ -329,15 +321,6 @@ impl LoggerBuilder {
self.uptime = uptime; self.uptime = uptime;
self self
} }
/// set the lowest loglevel to be displayed
///
/// Default: [`Level::INFO`]
#[must_use]
pub const fn set_level(mut self, max_level: Level) -> Self {
self.max_level = max_level;
self
}
} }
impl Default for LoggerBuilder { impl Default for LoggerBuilder {
@ -372,23 +355,19 @@ impl Default for LoggerBuilder {
/// ///
/// ## Levels /// ## Levels
/// ///
/// * [ERROR](Level::ERROR) Something broke /// TODO: add levels desc and ascii art
/// * [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 impossible or /// You can however use the [Logger] struct in cases where usage of a macro is bad 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.
/// ///
@ -470,6 +449,7 @@ impl Default for Logger {
} }
} }
fn new_file_appender(log_dir: PathBuf) -> tracing_appender::rolling::RollingFileAppender { fn new_file_appender(log_dir: PathBuf) -> NonBlocking {
tracing_appender::rolling::daily(log_dir, format!("{}.log", env!("CARGO_CRATE_NAME"))) let file_appender = tracing_appender::rolling::daily(log_dir, "log");
tracing_appender::non_blocking(file_appender).0
} }