Function flexi_logger::init [] [src]

pub fn init(config: LogConfig, loglevelspec: Option<String>) -> Result<()FlexiLoggerError>

Initializes the flexi_logger to your needs, and the global logger with flexi_logger.

Note: this should be called early in the execution of a Rust program. The global logger may only be initialized once, subsequent initialization attempts will return an error.

Configuration

See LogConfig for most of the initialization options.

Log Level Specification

Specifying the log levels that you really want to see in a specific program run can be done in the syntax defined by env_logger -> enabling logging (from where this functionality was ruthlessly copied). You can hand over the desired log-level-specification as an initialization parameter to flexi_logger, or, if you don't do so, with the environment variable RUST_LOG (as with env_logger). Since using environment variables is on Windows not as comfortable as on linux, you might consider using e.g. a docopt option for specifying the log-Level-specification on the command line of your program.

Examples

Use defaults only

If you initialize flexi_logger with default settings, then it behaves like env_logger:

use flexi_logger::{init,LogConfig};

init(LogConfig::new(), None).unwrap();

Write to files, use a detailed log-line format that contains the module and line number

Here we configure flexi_logger to write log entries with time and location info into a log file in folder "log_files", and we provide the loglevel-specification programmatically as a Some<String>, which might come in this form from what e.g. docopt could provide for a respective command-line option:

use flexi_logger::{init,opt_format,LogConfig};

init( LogConfig { log_to_file: true,
                  directory: Some("log_files".to_string()),
                  format: opt_format,
                  .. LogConfig::new() },
      Some("myprog=debug,mylib=warn".to_string()) )
.unwrap_or_else(|e|{panic!("Logger initialization failed with {}",e)});

Failures

Init returns a FlexiLoggerError, if it is supposed to write to an output file but the file cannot be opened, e.g. because of operating system issues.