Crate flexi_logger [] [src]

An extended version of the env_logger, which can write the log to standard error or to a fresh file and allows custom logline formats.

It plugs into the logging facade given by the log crate. See there to learn how to write traces from your code using simple macros.

Specifying the log levels that you really want to see in a specific program run happens in the same great way as with the env_logger (from where this functionality was ruthlessly copied), i.e., using the environment variable RUST_LOG.

Only the initialization is a bit more chatty due to the configurability.

Example: Initialization

If you initialize flexi_logger with default settings, then it behaves like the well-known env_logger:

use flexi_logger::{detailed_format, LogConfig};

    flexi_logger::init( LogConfig::new(), None )
            .unwrap_or_else(|e|{panic!("Logger initialization failed with {}",e)});

Here we configure flexi_logger to write log entries with fine-grained time and location info into a trace file, and we provide the loglevel-specification programmatically as a Some<String>, which fits well to what docopt provides, if you have e.g. a command-line option --loglevelspec:

use flexi_logger::{detailed_format, LogConfig};

    flexi_logger::init( LogConfig {
                            log_to_file: true,
                            format: flexi_logger::detailed_format,
                            .. LogConfig::new()  // use defaults for all other options
                        },
                        args.flag_loglevelspec
    ).unwrap_or_else(|e|{panic!("Logger initialization failed with {}",e)});

Flexi_logger comes with two predefined format variants, default_format() and detailed_format(), but you can easily create and use your own format function with the signature fn(&LogRecord) -> String.

Structs

FlexiLoggerError

Describes all kinds of errors in the initialization of FlexiLogger.

LogConfig

Allows influencing the behavior of the FlexiLogger.

LogRecord

The "payload" of a log message.

Functions

default_format

A logline-formatter that produces lines like
INFO [my_prog::some_submodel] Task successfully read from conf.json

detailed_format

A logline-formatter that produces lines like
[2015-07-08 12:12:32:639785] INFO [my_prog::some_submodel] src/some_submodel.rs:26: Task successfully read from conf.json

init

Initializes the global logger with a flexi logger.