pub struct SimpleLogger { /* 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 SimpleLogger

source

pub fn new() -> SimpleLogger

Initializes the global logger with a SimpleLogger instance with default log level set to Level::Trace.

use simple_logger::SimpleLogger;
SimpleLogger::new().env().init().unwrap();
log::warn!("This is an example message.");
Examples found in repository?
examples/init.rs (line 4)
3
4
5
6
7
fn main() {
    SimpleLogger::new().init().unwrap();

    log::warn!("This is an example message.");
}
More examples
Hide additional examples
examples/colors.rs (line 4)
3
4
5
6
7
fn main() {
    SimpleLogger::new().with_colors(true).init().unwrap();

    log::warn!("This is an example message.");
}
examples/timestamps_none.rs (line 4)
3
4
5
6
7
fn main() {
    SimpleLogger::new().without_timestamps().init().unwrap();

    log::warn!("This is an example message.");
}
examples/timestamps_utc.rs (line 4)
3
4
5
6
7
fn main() {
    SimpleLogger::new().with_utc_timestamps().init().unwrap();

    log::warn!("This is an example message.");
}
examples/timestamps_local.rs (line 4)
3
4
5
6
7
fn main() {
    SimpleLogger::new().with_local_timestamps().init().unwrap();

    log::warn!("This is an example message.");
}
examples/flush.rs (line 4)
3
4
5
6
7
8
9
fn main() {
    SimpleLogger::new().init().unwrap();

    log::warn!("This is an example message.");

    log::logger().flush();
}
source

pub fn from_env() -> SimpleLogger

👎Deprecated since 1.12.0: Use <a href="#method.env"><code>env</code></a> instead. Will be removed in version 2.0.0.

Simulates env_logger behavior, which enables the user to choose log level by setting a RUST_LOG environment variable. The RUST_LOG is not set or its value is not recognized as one of the log levels, this function will use the Error level by default.

You may use the various builder-style methods on this type to configure the logger, and you must call init in order to start logging messages.

use simple_logger::SimpleLogger;
SimpleLogger::from_env().init().unwrap();
log::warn!("This is an example message.");
source

pub fn env(self) -> SimpleLogger

Simulates env_logger behavior, which enables the user to choose log level by setting a RUST_LOG environment variable. This will use the default level set by with_level if RUST_LOG is not set or can’t be parsed as a standard log level.

This must be called after with_level. If called before with_level, it will have no effect.

Examples found in repository?
examples/timestamps_format.rs (line 6)
4
5
6
7
8
9
10
11
12
fn main() {
    SimpleLogger::new()
        .env()
        .with_timestamp_format(format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"))
        .init()
        .unwrap();

    log::warn!("This is an example message with custom timestamp format.");
}
source

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

Set the ‘default’ log level.

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

This must be called before env. If called after env, it will override the value loaded from the environment.

Examples found in repository?
examples/init_with_level.rs (line 5)
4
5
6
7
8
9
fn main() {
    SimpleLogger::new().with_level(LevelFilter::Warn).init().unwrap();

    log::warn!("This will be logged.");
    log::info!("This will NOT be logged.");
}
More examples
Hide additional examples
examples/init_with_target_level.rs (line 6)
4
5
6
7
8
9
10
11
12
fn main() {
    SimpleLogger::new()
        .with_level(LevelFilter::Info)
        .with_module_level("init_with_target_level", LevelFilter::Off)
        .init()
        .unwrap();

    log::info!("This will NOT be logged. (Target disabled)");
}
source

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

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 simple_logger::SimpleLogger;
use log::LevelFilter;

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

Disable logging for all dependencies:

use simple_logger::SimpleLogger;
use log::LevelFilter;

SimpleLogger::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)
4
5
6
7
8
9
10
11
12
fn main() {
    SimpleLogger::new()
        .with_level(LevelFilter::Info)
        .with_module_level("init_with_target_level", LevelFilter::Off)
        .init()
        .unwrap();

    log::info!("This will NOT be logged. (Target disabled)");
}
source

pub fn with_target_levels( self, target_levels: HashMap<String, LevelFilter> ) -> SimpleLogger

👎Deprecated since 1.11.0: Use <a href="#method.with_module_level"><code>with_module_level</code></a> instead. Will be removed in version 2.0.0.

Override the log level for specific targets.

source

pub fn with_timestamps(self, timestamps: bool) -> SimpleLogger

👎Deprecated since 1.16.0: Use [<code>with_local_timestamps</code>] or [<code>with_utc_timestamps</code>] instead. Will be removed in version 2.0.0.

Control whether timestamps are printed or not.

Timestamps will be displayed in the local timezone.

This method is only available if the timestamps feature is enabled.

source

pub fn with_timestamp_format( self, format: &'static [FormatItem<'static>] ) -> SimpleLogger

Control the format used for timestamps.

Without this, a default format is used depending on the timestamps type.

The syntax for the format_description macro can be found in the time crate book.

simple_logger::SimpleLogger::new()
 .with_level(log::LevelFilter::Debug)
 .env()
 .with_timestamp_format(time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"))
 .init()
 .unwrap();
Examples found in repository?
examples/timestamps_format.rs (line 7)
4
5
6
7
8
9
10
11
12
fn main() {
    SimpleLogger::new()
        .env()
        .with_timestamp_format(format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"))
        .init()
        .unwrap();

    log::warn!("This is an example message with custom timestamp format.");
}
source

pub fn without_timestamps(self) -> SimpleLogger

Don’t display any timestamps.

This method is only available if the timestamps feature is enabled.

Examples found in repository?
examples/timestamps_none.rs (line 4)
3
4
5
6
7
fn main() {
    SimpleLogger::new().without_timestamps().init().unwrap();

    log::warn!("This is an example message.");
}
source

pub fn with_local_timestamps(self) -> SimpleLogger

Display timestamps using the local timezone.

This method is only available if the timestamps feature is enabled.

Examples found in repository?
examples/timestamps_local.rs (line 4)
3
4
5
6
7
fn main() {
    SimpleLogger::new().with_local_timestamps().init().unwrap();

    log::warn!("This is an example message.");
}
source

pub fn with_utc_timestamps(self) -> SimpleLogger

Display timestamps using UTC.

This method is only available if the timestamps feature is enabled.

Examples found in repository?
examples/timestamps_utc.rs (line 4)
3
4
5
6
7
fn main() {
    SimpleLogger::new().with_utc_timestamps().init().unwrap();

    log::warn!("This is an example message.");
}
source

pub fn with_utc_offset(self, offset: UtcOffset) -> SimpleLogger

Display timestamps using a static UTC offset.

This method is only available if the timestamps feature is enabled.

Examples found in repository?
examples/timestamps_utc_offset.rs (line 6)
4
5
6
7
8
9
10
11
12
fn main() {
    SimpleLogger::new()
        .with_utc_offset(UtcOffset::from_hms(14, 0, 0).unwrap())
        .init()
        .unwrap();

    log::warn!("This is an example message using a static UTC offset.");
    log::info!("Daylight savings or other timezone changes will not be respected.");
}
source

pub fn with_colors(self, colors: bool) -> SimpleLogger

Control whether messages are colored or not.

This method is only available if the colored feature is enabled.

Examples found in repository?
examples/colors.rs (line 4)
3
4
5
6
7
fn main() {
    SimpleLogger::new().with_colors(true).init().unwrap();

    log::warn!("This is an example message.");
}
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)
3
4
5
6
7
fn main() {
    SimpleLogger::new().init().unwrap();

    log::warn!("This is an example message.");
}
More examples
Hide additional examples
examples/colors.rs (line 4)
3
4
5
6
7
fn main() {
    SimpleLogger::new().with_colors(true).init().unwrap();

    log::warn!("This is an example message.");
}
examples/timestamps_none.rs (line 4)
3
4
5
6
7
fn main() {
    SimpleLogger::new().without_timestamps().init().unwrap();

    log::warn!("This is an example message.");
}
examples/timestamps_utc.rs (line 4)
3
4
5
6
7
fn main() {
    SimpleLogger::new().with_utc_timestamps().init().unwrap();

    log::warn!("This is an example message.");
}
examples/timestamps_local.rs (line 4)
3
4
5
6
7
fn main() {
    SimpleLogger::new().with_local_timestamps().init().unwrap();

    log::warn!("This is an example message.");
}
examples/flush.rs (line 4)
3
4
5
6
7
8
9
fn main() {
    SimpleLogger::new().init().unwrap();

    log::warn!("This is an example message.");

    log::logger().flush();
}

Trait Implementations§

source§

impl Default for SimpleLogger

source§

fn default() -> Self

See this

source§

impl Log for SimpleLogger

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.

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.