Function log::set_logger [] [src]

pub fn set_logger(logger: &'static Log) -> Result<(), SetLoggerError>

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.

Errors

An error is returned if a logger has already been set.

Examples

use log::{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");