Struct env_logger::Builder
[−]
[src]
pub struct Builder { /* fields omitted */ }
Builder
acts as builder for initializing a 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 std::io::Write; use log::LevelFilter; use env_logger::Builder; fn main() { let mut builder = Builder::new(); builder.format(|buf, record| writeln!(buf, "{} - {}", record.level(), record.args())) .filter(None, LevelFilter::Info); if let Ok(rust_log) = env::var("RUST_LOG") { builder.parse(&rust_log); } builder.init(); error!("error message"); info!("info message"); }
Methods
impl Builder
[src]
fn new() -> Builder
[src]
Initializes the log builder with defaults.
fn filter(&mut self, module: Option<&str>, level: LevelFilter) -> &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.
fn format<F: 'static>(&mut self, format: F) -> &mut Self where
F: Fn(&mut Formatter, &Record) -> Result<()> + Sync + Send,
[src]
F: Fn(&mut Formatter, &Record) -> Result<()> + Sync + Send,
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.
fn target(&mut self, target: Target) -> &mut Self
[src]
Sets the target for the log output.
Env logger can log to either stdout or stderr. The default is stderr.
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.
fn try_init(&mut self) -> Result<(), SetLoggerError>
[src]
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.
fn init(&mut self)
[src]
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.
fn build(&mut self) -> Logger
[src]
Build an env logger.