Struct IcLogger

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

Implements Log and a set of simple builder methods for configuration.

Use the various “builder” methods on this struct to configure the logger, then call init to configure the log crate.

Implementations§

Source§

impl IcLogger

Source

pub fn new() -> IcLogger

Initializes the global logger with a IcLogger instance with default log level set to Level::Warn.

use ic_logger::IcLogger;
IcLogger::new().init().unwrap();
log::warn!("This is an example message.");
Examples found in repository?
examples/init.rs (line 4)
3fn main() {
4    IcLogger::new().init().unwrap();
5
6    log::warn!("This is an example message.");
7}
More examples
Hide additional examples
examples/flush.rs (line 4)
3fn main() {
4    IcLogger::new().init().unwrap();
5
6    log::warn!("This is an example message.");
7
8    log::logger().flush();
9}
examples/init_with_level.rs (line 5)
4fn main() {
5    IcLogger::new().with_level(LevelFilter::Warn).init().unwrap();
6
7    log::warn!("This will be logged.");
8    log::info!("This will NOT be logged.");
9}
examples/init_with_target_level.rs (line 5)
4fn main() {
5    IcLogger::new()
6        .with_level(LevelFilter::Info)
7        .with_module_level("init_with_target_level", LevelFilter::Off)
8        .init()
9        .unwrap();
10
11    log::info!("This will NOT be logged. (Target disabled)");
12}
Source

pub fn with_level(self, level: LevelFilter) -> IcLogger

Set the ‘default’ log level.

You can override the default level for specific modules and their sub-modules using with_module_level

Examples found in repository?
examples/init_with_level.rs (line 5)
4fn main() {
5    IcLogger::new().with_level(LevelFilter::Warn).init().unwrap();
6
7    log::warn!("This will be logged.");
8    log::info!("This will NOT be logged.");
9}
More examples
Hide additional examples
examples/init_with_target_level.rs (line 6)
4fn main() {
5    IcLogger::new()
6        .with_level(LevelFilter::Info)
7        .with_module_level("init_with_target_level", LevelFilter::Off)
8        .init()
9        .unwrap();
10
11    log::info!("This will NOT be logged. (Target disabled)");
12}
Source

pub fn with_module_level(self, target: &str, level: LevelFilter) -> IcLogger

Override the log level for some specific modules.

This sets the log level of a specific module and all its sub-modules. When both the level for a parent module as well as a child module are set, the more specific value is taken. If the log level for the same module is specified twice, the resulting log level is implementation defined.

§Examples

Silence an overly verbose crate:

use ic_logger::IcLogger;
use log::LevelFilter;

IcLogger::new().with_module_level("chatty_dependency", LevelFilter::Warn).init().unwrap();

Disable logging for all dependencies:

use ic_logger::IcLogger;
use log::LevelFilter;

IcLogger::new()
    .with_level(LevelFilter::Off)
    .with_module_level("my_crate", LevelFilter::Info)
    .init()
    .unwrap();
Examples found in repository?
examples/init_with_target_level.rs (line 7)
4fn main() {
5    IcLogger::new()
6        .with_level(LevelFilter::Info)
7        .with_module_level("init_with_target_level", LevelFilter::Off)
8        .init()
9        .unwrap();
10
11    log::info!("This will NOT be logged. (Target disabled)");
12}
Source

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

‘Init’ the actual logger, instantiate it and configure it, this method MUST be called in order for the logger to be effective.

Examples found in repository?
examples/init.rs (line 4)
3fn main() {
4    IcLogger::new().init().unwrap();
5
6    log::warn!("This is an example message.");
7}
More examples
Hide additional examples
examples/flush.rs (line 4)
3fn main() {
4    IcLogger::new().init().unwrap();
5
6    log::warn!("This is an example message.");
7
8    log::logger().flush();
9}
examples/init_with_level.rs (line 5)
4fn main() {
5    IcLogger::new().with_level(LevelFilter::Warn).init().unwrap();
6
7    log::warn!("This will be logged.");
8    log::info!("This will NOT be logged.");
9}
examples/init_with_target_level.rs (line 8)
4fn main() {
5    IcLogger::new()
6        .with_level(LevelFilter::Info)
7        .with_module_level("init_with_target_level", LevelFilter::Off)
8        .init()
9        .unwrap();
10
11    log::info!("This will NOT be logged. (Target disabled)");
12}

Trait Implementations§

Source§

impl Default for IcLogger

Source§

fn default() -> Self

See this

Source§

impl Log for IcLogger

Source§

fn enabled(&self, metadata: &Metadata<'_>) -> bool

Determines if a log message with the specified metadata would be logged. Read more
Source§

fn log(&self, record: &Record<'_>)

Logs the Record. Read more
Source§

fn flush(&self)

Flushes any buffered records. Read more

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> Same for T

Source§

type Output = T

Should always be Self
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.