Struct flexi_logger::Logger [] [src]

pub struct Logger { /* fields omitted */ }

The standard entry-point for using flexi_logger.

Create a Logger with your desired loglevel-specification

use its configuration methods, and finally call start().

Examples

Use defaults only

If you initialize flexi_logger like this, it behaves like env_logger:

use flexi_logger::Logger;

Logger::with_env().start().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 String:

use flexi_logger::{Logger,opt_format};

Logger::with_str("myprog=debug, mylib=warn")
            .log_to_file()
            .directory("log_files")
            .format(opt_format)
            .start()
            .unwrap_or_else(|e|{panic!("Logger initialization failed with {}",e)});

Methods

impl Logger
[src]

Simple methods for influencing the behavior of the Logger.

Create a Logger that you provide with an explicit LogSpecification.

Create a Logger that reads the LogSpecification from a String or &str.

Create a Logger that reads the LogSpecification from the environment variable RUST_LOG.

Makes the logger write all logs to a file, rather than to stderr.

Makes the logger print an info message to stdout when a new file is used for log-output.

Makes the logger write all logged error messages additionally to stdout.

Makes the logger write all logged error, warning, and info messages additionally to stdout.

Makes the logger use the provided format function for the log entries, rather than the default (formats::default_format).

Specifies a folder for the log files.

This parameter only has an effect if log_to_file is set to true. If the specified folder does not exist, the initialization will fail. By default, the log files are created in the folder where the program was started.

Specifies a suffix for the log files.

This parameter only has an effect if log_to_file is set to true.

Makes the logger not include a timestamp into the names of the log files (log_to_file must be chosen, too).

This option only has an effect if log_to_file is used, too.

By default, the log file will grow indefinitely. With this option, when the log file reaches or exceeds the specified file size, the file will be closed and a new file will be opened. Also he filename pattern changes - instead of the timestamp a serial number is included into the filename.

This option only has an effect if log_to_file is used, too.

The specified String is added to the log file name.

This option only has an effect if log_to_file is used, too.

The specified String will be used on linux systems to create in the current folder a symbolic link to the current log file.

Consumes the Logger object and initializes the flexi_logger.

Consumes the Logger object and initializes the flexi_logger in a way that subsequently the log specification can be exchanged dynamically.

The resulting logger is still fast, but measurable slower for those log-calls (trace!() etc) that are on a deeper level than the deepest level in the LogSpecification. This is because the Log crate has an optimization for returning very fast from deep-level log calls, but the deepest level needs be given at initialization and cannot be updated later.

Here is the output from a benchmark test, runnning on a windows laptop:

  1   PS C:\dev\rust\projects\flexi_logger> cargo bench --test bench_standard -- --nocapture
  2       Finished release [optimized] target(s) in 0.0 secs
  3        Running target\release\deps\bench_standard-158f621674f85c86.exe
  4
  5   running 4 tests
  6   test b10_no_logger_active  ... bench:         136 ns/iter (+/- 30)
  7   test b20_initialize_logger ... bench:           0 ns/iter (+/- 0)
  8   test b30_relevant_logs     ... bench:   1,676,793 ns/iter (+/- 342,747)
  9   test b40_suppressed_logs   ... bench:         134 ns/iter (+/- 5)
 10
 11   test result: ok. 0 passed; 0 failed; 0 ignored; 4 measured; 0 filtered out
 12
 13   PS C:\dev\rust\projects\flexi_logger> cargo bench --test bench_reconfigurable -- --nocapture
 14       Finished release [optimized] target(s) in 0.0 secs
 15        Running target\release\deps\bench_reconfigurable-bc6bb7d69906fc2f.exe
 16
 17   running 4 tests
 18   test b10_no_logger_active  ... bench:         134 ns/iter (+/- 19)
 19   test b20_initialize_logger ... bench:           0 ns/iter (+/- 0)
 20   test b30_relevant_logs     ... bench:   1,665,871 ns/iter (+/- 306,734)
 21   test b40_suppressed_logs   ... bench:       5,208 ns/iter (+/- 564)
 22
 23   test result: ok. 0 passed; 0 failed; 0 ignored; 4 measured; 0 filtered out

It shows that logging is fastest when no logger is active (lines 6 and 18). And it is just as fast when the above-mentioned optimization kicks in (line 9).

Logging is expensive when logs are really written (line 8 and 20), independent of the reconfigurability feature of the flexi_logger.

The measurable, but still in most cases not important price for reconfigurability can be seen by comparing lines 9 and 21.

impl Logger
[src]

Alternative set of methods to control the behavior of the Logger. use these methods when you want to control the settings dynamically, e.g. with doc_opts.

With true, makes the logger write all logs to a file, otherwise to stderr.

With true, makes the logger print an info message to stdout when a new file is used for log-output.

With true, makes the logger write all logged error messages additionally to stdout.

With true, makes the logger write all logged error, warning, and info messages additionally to stdout.

Specifies a folder for the log files.

This parameter only has an effect if log_to_file is set to true. If the specified folder does not exist, the initialization will fail. With None, the log files are created in the folder where the program was started.

Specifies a suffix for the log files.

This parameter only has an effect if log_to_file is set to true. By default, the suffix 'log' is used. With None, no suffix is used.

With true, makes the logger include a timestamp into the names of the log files. (log_to_file must be chosen, too).

This option only has an effect if log_to_file is used, too.

By default, and with None, the log file will grow indefinitely. If a size is set, when the log file reaches or exceeds the specified size, the file will be closed and a new file will be opened. Also the filename pattern changes - instead of the timestamp a serial number is included into the filename.

This option only has an effect if log_to_file is used, too.

The specified String is added to the log file name.

This option only has an effect if log_to_file is used, too.

If a String is specified, it will be used on linux systems to create in the current folder a symbolic link with this name to the current log file.