Skip to main content

LoggerBuilder

Struct LoggerBuilder 

Source
pub struct LoggerBuilder { /* private fields */ }
Expand description

Constructs an nice-log logger.

Implementations§

Source§

impl LoggerBuilder

Source

pub fn new(max_log_level: LevelFilter) -> Self

Create a builder for a logger. The logger can be installed using the build_global() function.

Examples found in repository?
examples/filtered.rs (line 4)
3fn main() {
4    nice_log::LoggerBuilder::new(LevelFilter::Trace)
5        // Filtering only works with exact matches, so the log messages from
6        // `some_module::some_sub_module` will still show up
7        .filter_module("filtered::some_module")
8        .build_global()
9        .expect("A logger has already been set up");
10
11    some_module::log();
12    some_module::some_sub_module::log();
13}
More examples
Hide additional examples
examples/basic.rs (line 6)
3fn main() {
4    // The log output is determined by the `NICE_LOG` environment variable as dedicated by the rules
5    // outlined in the repository's readme
6    nice_log::LoggerBuilder::new(LevelFilter::Trace)
7        .build_global()
8        // In this example something would be have gone very wrong if we cannot set up the logger.
9        // If there however is a possibility that the logger is configured multiple times then this
10        // error should be handled appropriately.
11        .expect("A logger has already been set up");
12
13    // When changing some of the level filter above some of these messages may no longer be printed
14    log::error!("This is an error");
15    log::warn!("This is a warning");
16    log::info!("This is a regular log message");
17    log::debug!("This is a debug message, usually only made visible during debug builds");
18    log::trace!("This is a trace message, usually hidden unless explicitly opted into");
19
20    // Debug and trace messages contain the module path
21    some_module::log_from_module();
22}
Source

pub fn build_global(self) -> Result<(), SetLoggerError>

Install the configured logger as the global logger. The global logger can only be set once.

Examples found in repository?
examples/filtered.rs (line 8)
3fn main() {
4    nice_log::LoggerBuilder::new(LevelFilter::Trace)
5        // Filtering only works with exact matches, so the log messages from
6        // `some_module::some_sub_module` will still show up
7        .filter_module("filtered::some_module")
8        .build_global()
9        .expect("A logger has already been set up");
10
11    some_module::log();
12    some_module::some_sub_module::log();
13}
More examples
Hide additional examples
examples/basic.rs (line 7)
3fn main() {
4    // The log output is determined by the `NICE_LOG` environment variable as dedicated by the rules
5    // outlined in the repository's readme
6    nice_log::LoggerBuilder::new(LevelFilter::Trace)
7        .build_global()
8        // In this example something would be have gone very wrong if we cannot set up the logger.
9        // If there however is a possibility that the logger is configured multiple times then this
10        // error should be handled appropriately.
11        .expect("A logger has already been set up");
12
13    // When changing some of the level filter above some of these messages may no longer be printed
14    log::error!("This is an error");
15    log::warn!("This is a warning");
16    log::info!("This is a regular log message");
17    log::debug!("This is a debug message, usually only made visible during debug builds");
18    log::trace!("This is a trace message, usually hidden unless explicitly opted into");
19
20    // Debug and trace messages contain the module path
21    some_module::log_from_module();
22}
Source

pub fn always_show_module_path(self) -> Self

Always show the module path. Normally this is only shown for the messages on the Debug level or on higher verbosity levels. Useful for debugging.

Source

pub fn filter_crate(self, crate_name: impl Into<String>) -> Self

Filter out log messages produced by the given crate.

Source

pub fn filter_module(self, crate_name: impl Into<String>) -> Self

Filter out log messages produced by the given module. Module names are matched exactly and case sensitively. Filtering based on a module prefix is currently not supported.

Examples found in repository?
examples/filtered.rs (line 7)
3fn main() {
4    nice_log::LoggerBuilder::new(LevelFilter::Trace)
5        // Filtering only works with exact matches, so the log messages from
6        // `some_module::some_sub_module` will still show up
7        .filter_module("filtered::some_module")
8        .build_global()
9        .expect("A logger has already been set up");
10
11    some_module::log();
12    some_module::some_sub_module::log();
13}
Source

pub fn with_output_target( self, target: OutputTarget, ) -> Result<Self, SetTargetError>

Explicitly set the output target for the logger. This is normally set using the NICE_LOG environment variable. Returns an error if the target could not be set.

Trait Implementations§

Source§

impl Debug for LoggerBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<SetTargetError> for LoggerBuilder

Source§

fn from(value: SetTargetError) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.