pub struct SimpleLogger {
pub(crate) default_level: LevelFilter,
pub(crate) module_levels: Vec<(String, LevelFilter)>,
pub(crate) timestamps: Timestamps,
pub(crate) timestamps_format: Option<&'static [FormatItem<'static>]>,
pub(crate) colors: bool,
}
Expand description
Fields§
§default_level: LevelFilter
The default logging level
module_levels: Vec<(String, LevelFilter)>
The specific logging level for each module
This is used to override the default value for some specific modules.
This must be sorted from most-specific to least-specific, so that enabled
can scan the
vector for the first match to give us the desired log level for a module.
timestamps: Timestamps
Control how timestamps are displayed.
This field is only available if the timestamps
feature is enabled.
timestamps_format: Option<&'static [FormatItem<'static>]>
§colors: bool
Whether to use color output or not.
This field is only available if the color
feature is enabled.
Implementations§
source§impl SimpleLogger
impl SimpleLogger
sourcepub fn new() -> SimpleLogger
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.");
sourcepub fn from_env() -> SimpleLogger
👎Deprecated since 1.12.0: Use env
instead. Will be removed in version 2.0.0.
pub fn from_env() -> SimpleLogger
env
instead. Will be removed in version 2.0.0.Initializes the global logger with log level read from RUST_LOG
environment
variable value. Deprecated in favor of env()
.
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.");
sourcepub fn env(self) -> SimpleLogger
pub fn env(self) -> SimpleLogger
Enables the user to choose log level by setting RUST_LOG=<level>
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.
sourcepub fn with_level(self, level: LevelFilter) -> SimpleLogger
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.
sourcepub fn with_module_level(self, target: &str, level: LevelFilter) -> SimpleLogger
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();
sourcepub fn with_target_levels(
self,
target_levels: HashMap<String, LevelFilter>,
) -> SimpleLogger
👎Deprecated since 1.11.0: Use with_module_level
instead. Will be removed in version 2.0.0.
pub fn with_target_levels( self, target_levels: HashMap<String, LevelFilter>, ) -> SimpleLogger
with_module_level
instead. Will be removed in version 2.0.0.Override the log level for specific targets.
sourcepub fn with_timestamps(self, timestamps: bool) -> SimpleLogger
👎Deprecated since 1.16.0: Use [with_local_timestamps
] or [with_utc_timestamps
] instead. Will be removed in version 2.0.0.
pub fn with_timestamps(self, timestamps: bool) -> SimpleLogger
with_local_timestamps
] or [with_utc_timestamps
] 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.
sourcepub fn with_timestamp_format(
self,
format: &'static [FormatItem<'static>],
) -> SimpleLogger
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();
sourcepub fn without_timestamps(self) -> SimpleLogger
pub fn without_timestamps(self) -> SimpleLogger
Don’t display any timestamps.
This method is only available if the timestamps
feature is enabled.
sourcepub fn with_local_timestamps(self) -> SimpleLogger
pub fn with_local_timestamps(self) -> SimpleLogger
Display timestamps using the local timezone.
This method is only available if the timestamps
feature is enabled.
sourcepub fn with_utc_timestamps(self) -> SimpleLogger
pub fn with_utc_timestamps(self) -> SimpleLogger
Display timestamps using UTC.
This method is only available if the timestamps
feature is enabled.
sourcepub fn with_utc_offset(self, offset: UtcOffset) -> SimpleLogger
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.
sourcepub fn with_colors(self, colors: bool) -> SimpleLogger
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.
sourcepub fn max_level(&self) -> LevelFilter
pub fn max_level(&self) -> LevelFilter
Configure the logger
sourcepub fn init(self) -> Result<(), SetLoggerError>
pub fn init(self) -> Result<(), SetLoggerError>
‘Init’ the actual logger and instantiate it, this method MUST be called in order for the logger to be effective.