Function log::set_logger
source · pub fn set_logger(logger: &'static dyn Log) -> Result<(), SetLoggerError>
Expand description
Sets the global logger to a &'static Log
.
This function may only be called once in the lifetime of a program. Any log
events that occur before the call to set_logger
completes will be ignored.
This function does not typically need to be called manually. Logger implementations should provide an initialization method that installs the logger internally.
§Availability
This method is available even when the std
feature is disabled. However,
it is currently unavailable on thumbv6
targets, which lack support for
some atomic operations which are used by this function. Even on those
targets, set_logger_racy
will be available.
§Errors
An error is returned if a logger has already been set.
§Examples
use log::{error, info, warn, Record, Level, Metadata, LevelFilter};
static MY_LOGGER: MyLogger = MyLogger;
struct MyLogger;
impl log::Log for MyLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Info
}
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
println!("{} - {}", record.level(), record.args());
}
}
fn flush(&self) {}
}
log::set_logger(&MY_LOGGER).unwrap();
log::set_max_level(LevelFilter::Info);
info!("hello log");
warn!("warning");
error!("oops");