[][src]Struct env_logger::Builder

pub struct Builder { /* fields omitted */ }

Builder acts as builder for initializing a Logger.

It can be used to customize the log format, change the environment variable used to provide the logging directives and also set the default log level filter.

Examples

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

use std::env;
use std::io::Write;
use log::LevelFilter;
use env_logger::Builder;

fn main() {
    let mut builder = Builder::from_default_env();

    builder.format(|buf, record| writeln!(buf, "{} - {}", record.level(), record.args()))
           .filter(None, LevelFilter::Info)
           .init();

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

Methods

impl Builder
[src]

Initializes the log builder with defaults.

NOTE: This method won't read from any environment variables. Use the filter and write_style methods to configure the builder or use from_env or from_default_env instead.

Examples

Create a new builder and configure filters and style:

use log::LevelFilter;
use env_logger::{Builder, WriteStyle};
 
let mut builder = Builder::new();
 
builder.filter(None, LevelFilter::Info)
       .write_style(WriteStyle::Always)
       .init();

Initializes the log builder from the environment.

The variables used to read configuration from can be tweaked before passing in.

Examples

Initialise a logger reading the log filter from an environment variable called MY_LOG:

use env_logger::Builder;
 
let mut builder = Builder::from_env("MY_LOG");
builder.init();

Initialise a logger using the MY_LOG variable for filtering and MY_LOG_STYLE for whether or not to write styles:

use env_logger::{Builder, Env};

let env = Env::new().filter("MY_LOG").write_style("MY_LOG_STYLE");

let mut builder = Builder::from_env(env);
builder.init();

Initializes the log builder from the environment using default variable names.

This method is a convenient way to call from_env(Env::default()) without having to use the Env type explicitly. The builder will use the default environment variables.

Examples

Initialise a logger using the default environment variables:

use env_logger::Builder;
 
let mut builder = Builder::from_default_env();
builder.init();

Sets the format function for formatting the log output.

This function is called on each record logged and should format the log record and output it to the given Formatter.

The format function is expected to output the string directly to the Formatter so that implementations can use the std::fmt macros to format and output without intermediate heap allocations. The default env_logger formatter takes advantage of this.

Examples

Use a custom format to write only the log message:

use std::io::Write;
use env_logger::Builder;
 
let mut builder = Builder::new();
 
builder.format(|buf, record| writeln!(buf, "{}", record.args()));

Use the default format.

This method will clear any custom format set on the builder.

Whether or not to write the level in the default format.

Whether or not to write the module path in the default format.

Whether or not to write the timestamp in the default format.

Whether or not to write the timestamp with nanos.

Adds a directive to the filter for a specific module.

Examples

Only include messages for warning and above for logs in path::to::module:

use log::LevelFilter;
use env_logger::Builder;

let mut builder = Builder::new();

builder.filter_module("path::to::module", LevelFilter::Info);

Adds a directive to the filter for all modules.

Examples

Only include messages for warning and above for logs in path::to::module:

use log::LevelFilter;
use env_logger::Builder;

let mut builder = Builder::new();

builder.filter_level(LevelFilter::Info);

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.

Examples

Only include messages for warning and above for logs in path::to::module:

use log::LevelFilter;
use env_logger::Builder;
 
let mut builder = Builder::new();
 
builder.filter(Some("path::to::module"), LevelFilter::Info);

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

See the module documentation for more details.

Sets the target for the log output.

Env logger can log to either stdout or stderr. The default is stderr.

Examples

Write log message to stdout:

use env_logger::{Builder, Target};
 
let mut builder = Builder::new();
 
builder.target(Target::Stdout);

Sets whether or not styles will be written.

This can be useful in environments that don't support control characters for setting colors.

Examples

Never attempt to write styles:

use env_logger::{Builder, WriteStyle};
 
let mut builder = Builder::new();
 
builder.write_style(WriteStyle::Never);

Parses whether or not to write styles in the same form as the RUST_LOG_STYLE environment variable.

See the module documentation for more details.

Initializes the global logger with the built env logger.

This should be called early in the execution of a Rust program. Any log events that occur before initialization will be ignored.

Errors

This function will fail if it is called more than once, or if another library has already initialized a global logger.

Initializes the global logger with the built env logger.

This should be called early in the execution of a Rust program. Any log events that occur before initialization will be ignored.

Panics

This function will panic if it is called more than once, or if another library has already initialized a global logger.

Build an env logger.

The returned logger implements the Log trait and can be installed manually or nested within another logger.

Trait Implementations

impl Default for Builder
[src]

Returns the "default value" for a type. Read more

impl Debug for Builder
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl Send for Builder

impl Sync for Builder

Blanket Implementations

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

Performs the conversion.

impl<T> From for T
[src]

Performs the conversion.

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

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

Immutably borrows from an owned value. Read more

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

Mutably borrows from an owned value. Read more

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

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

🔬 This is a nightly-only experimental API. (get_type_id)

this method will likely be replaced by an associated static

Gets the TypeId of self. Read more