[][src]Struct env_logger::LogBuilder

pub struct LogBuilder { /* fields omitted */ }

LogBuilder acts as builder for initializing the Logger. It can be used to customize the log format, change the enviromental variable used to provide the logging directives and also set the default log level filter.

Example

#[macro_use]
extern crate log;
extern crate env_logger;

use std::env;
use log::{LogRecord, LogLevelFilter};
use env_logger::LogBuilder;

fn main() {
    let format = |record: &LogRecord| {
        format!("{} - {}", record.level(), record.args())
    };

    let mut builder = LogBuilder::new();
    builder.format(format).filter(None, LogLevelFilter::Info);

    if env::var("RUST_LOG").is_ok() {
       builder.parse(&env::var("RUST_LOG").unwrap());
    }

    builder.init().unwrap();

    error!("error message");
    info!("info message");
}

Methods

impl LogBuilder[src]

pub fn new() -> LogBuilder[src]

Initializes the log builder with defaults

pub fn filter(
    &mut self,
    module: Option<&str>,
    level: LogLevelFilter
) -> &mut Self
[src]

Adds filters to the logger

The given module (if any) will log at most the specified level provided. If no module is provided then the filter will apply to all log messages.

pub fn format<F: 'static>(&mut self, format: F) -> &mut Self where
    F: Fn(&LogRecord) -> String + Sync + Send
[src]

Sets the format function for formatting the log output.

This function is called on each record logged to produce a string which is actually printed out.

pub fn parse(&mut self, filters: &str) -> &mut Self[src]

Parses the directives string in the same form as the RUST_LOG environment variable.

See the module documentation for more details.

pub fn init(&mut self) -> Result<(), SetLoggerError>[src]

Initializes the global logger with an env logger.

This should be called early in the execution of a Rust program, and the global logger may only be initialized once. Future initialization attempts will return an error.

pub fn build(&mut self) -> Logger[src]

Build an env logger.

Auto Trait Implementations

impl Send for LogBuilder

impl Sync for LogBuilder

Blanket Implementations

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.