[][src]Struct flexi_logger::Logger

pub struct Logger { /* fields omitted */ }

The entry-point for using flexi_logger.

A simple example with file logging might look like this:

use flexi_logger::{Duplicate,Logger};

Logger::with_str("info, mycrate = debug")
        .log_to_file()
        .duplicate_to_stderr(Duplicate::Warn)
        .start()
        .unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));

Logger is a builder class that allows you to

Methods

impl Logger[src]

Create a Logger instance and define how to access the (initial) loglevel-specification.

pub fn with(logspec: LogSpecification) -> Logger[src]

Creates a Logger that you provide with an explicit LogSpecification. By default, logs are written with default_format to stderr.

pub fn with_str<S: AsRef<str>>(s: S) -> Logger[src]

Creates a Logger that reads the LogSpecification from a String or &str. See LogSpecification for the syntax.

pub fn with_env() -> Logger[src]

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

pub fn with_env_or_str<S: AsRef<str>>(s: S) -> Logger[src]

Creates a Logger that reads the LogSpecification from the environment variable RUST_LOG, or derives it from the given String, if RUST_LOG is not set.

impl Logger[src]

Simple methods for influencing the behavior of the Logger.

pub fn check_parser_error(self) -> Result<Logger, FlexiLoggerError>[src]

Allows verifying that no parsing errors have occured in the used factory method, and examining the parse error.

Most of the factory methods for Logger (Logger::with_...()) parse a log specification String, and deduce from it a LogSpecification object. If parsing fails, errors are reported to stdout, but effectively ignored. In worst case, nothing is logged!

This method gives programmatic access to parse errors, if there were any, so that errors don't happen unnoticed.

In the following example we just panic if the spec was not free of errors:

Logger::with_str("hello world")
.check_parser_error()
.unwrap()       // <-- here we could do better than panic
.log_to_file()
.start();

pub fn log_to_file(self) -> Logger[src]

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

The default pattern for the filename is '<program_name>_<date>_<time>.<suffix>', e.g. myprog_2015-07-08_10-44-11.log.

pub fn do_not_log(self) -> Logger[src]

Makes the logger write no logs at all.

This can be useful when you want to run tests of your programs with all log-levels active to ensure the log calls which are normally not active will not cause undesired side-effects when activated (note that the log macros prevent arguments of inactive log-calls from being evaluated).

pub fn print_message(self) -> Logger[src]

Makes the logger print an info message to stdout with the name of the logfile when a logfile is opened for writing.

pub fn duplicate_to_stderr(self, dup: Duplicate) -> Logger[src]

Makes the logger write messages with the specified minimum severity additionally to stderr.

pub fn format(self, format: FormatFunction) -> Logger[src]

Makes the logger use the provided format function for all messages that are written to files or to stderr.

You can either choose one of the provided log-line formatters, or you create and use your own format function with the signature
fn(&Record) -> String.

By default, default_format() is used for the output to files and colored_default_format() is used for the output to stderr.

If the feature colors is switched off, default_format() is used for all outputs.

pub fn format_for_files(self, format: FormatFunction) -> Logger[src]

Makes the logger use the provided format function for messages that are written to files.

Regarding the default, see Logger::format().

pub fn format_for_stderr(self, format: FormatFunction) -> Logger[src]

Makes the logger use the provided format function for messages that are written to stderr.

Regarding the default, see Logger::format().

pub fn directory<S: Into<PathBuf>>(self, directory: S) -> Logger[src]

Specifies a folder for the log files.

This parameter only has an effect if log_to_file() is used, too. 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.

pub fn suffix<S: Into<String>>(self, suffix: S) -> Logger[src]

Specifies a suffix for the log files.

This parameter only has an effect if log_to_file() is used, too.

pub fn suppress_timestamp(self) -> Logger[src]

Makes the logger not include a timestamp into the names of the log files.

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

pub fn rotate(
    self,
    criterion: Criterion,
    naming: Naming,
    cleanup: Cleanup
) -> Logger
[src]

Prevent indefinite growth of the log file by applying file rotation and a clean-up strategy for older log files.

By default, the log file is fixed while your program is running and will grow indefinitely. With this option being used, when the log file reaches the specified criterion, the file will be closed and a new file will be opened.

Note that also the filename pattern changes:

  • by default, no timestamp is added to the filename
  • the logs are always written to a file with infix _rCURRENT
  • when the rotation criterion is fulfilled, it is closed and renamed to a file with another infix (see Naming), and then the logging continues again to the (fresh) file with infix _rCURRENT.

Example:

After some logging with your program my_prog and rotation with Naming::Numbers, you will find files like

my_prog_r00000.log
my_prog_r00001.log
my_prog_r00002.log
my_prog_rCURRENT.log

Parameters

rotate_over_size is given in bytes, e.g. 10_000_000 will rotate files once they reach a size of 10 MiB. cleanup defines the strategy for dealing with older files. See Cleanup for details.

pub fn append(self) -> Logger[src]

Makes the logger append to the specified output file, if it exists already; by default, the file would be truncated.

This option only has an effect if log_to_file() is used, too. This option will hardly make an effect if suppress_timestamp() is not used.

pub fn discriminant<S: Into<String>>(self, discriminant: S) -> Logger[src]

The specified String is added to the log file name after the program name.

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

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

This method has no effect on filesystems where symlinks are not supported. This option only has an effect if log_to_file() is used, too.

pub fn add_writer<S: Into<String>>(
    self,
    name: S,
    writer: Box<dyn LogWriter>
) -> Logger
[src]

Registers a LogWriter implementation under the given target name.

The target name should not start with an underscore.

See the module documentation of writers.

pub fn use_windows_line_ending(self) -> Logger[src]

Use Windows line endings, rather than just \n.

impl Logger[src]

Alternative set of methods to control the behavior of the Logger. Use these methods when you want to control the settings flexibly, e.g. with commandline arguments via docopts or clap.

pub fn o_log_to_file(self, log_to_file: bool) -> Logger[src]

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

pub fn o_print_message(self, print_message: bool) -> Logger[src]

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

pub fn o_directory<P: Into<PathBuf>>(self, directory: Option<P>) -> Logger[src]

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.

pub fn o_rotate(
    self,
    rotate_config: Option<(Criterion, Naming, Cleanup)>
) -> Logger
[src]

By default, and with None, the log file will grow indefinitely. If a rotate_config 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.

The size is given in bytes, e.g. o_rotate_over_size(Some(1_000)) will rotate files once they reach a size of 1 kB.

The cleanup strategy allows delimiting the used space on disk.

pub fn o_timestamp(self, timestamp: bool) -> Logger[src]

With true, makes the logger include a timestamp into the names of the log files. true is the default, but rotate_over_size sets it to false. With this method you can set it to true again.

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

pub fn o_append(self, append: bool) -> Logger[src]

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

If append is set to true, makes the logger append to the specified output file, if it exists. By default, or with false, the file would be truncated.

This option will hardly make an effect if suppress_timestamp() is not used.

pub fn o_discriminant<S: Into<String>>(self, discriminant: Option<S>) -> Logger[src]

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

The specified String is added to the log file name.

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

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.

impl Logger[src]

Finally, start logging, optionally with a spec-file.

pub fn start(self) -> Result<ReconfigurationHandle, FlexiLoggerError>[src]

Consumes the Logger object and initializes flexi_logger.

The returned reconfiguration handle allows updating the log specification programmatically later on, e.g. to intensify logging for (buggy) parts of a (test) program, etc. See ReconfigurationHandle for an example.

pub fn start_with_specfile<P: AsRef<Path>>(
    self,
    specfile: P
) -> Result<(), FlexiLoggerError>
[src]

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

Uses the spec that was given to the factory method (Logger::with() etc) as initial spec and then tries to read the logspec from a file.

If the file does not exist, flexi_logger creates the file and fills it with the initial spec (and in the respective file format, of course).

Feature dependency

The implementation of this configuration method uses some additional crates that you might not want to depend on with your program if you don't use this functionality. For that reason the method is only available if you activate the specfile feature. See flexi_logger's usage section for details.

Usage

A logger initialization like

This example is not tested
use flexi_logger::Logger;
    Logger::with_str("info")/*...*/.start_with_specfile("logspecification.toml");

will create the file logspecification.toml (if it does not yet exist) with this content:

### Optional: Default log level
global_level = 'info'
### Optional: specify a regular expression to suppress all messages that don't match
#global_pattern = 'foo'

### Specific log levels per module are optionally defined in this section
[modules]
#'mod1' = 'warn'
#'mod2' = 'debug'
#'mod2::mod3' = 'trace'

You can subsequently edit and modify the file according to your needs, while the program is running, and it will immediately take your changes into account.

Currently only toml-files are supported, the file suffix thus must be .toml.

The initial spec remains valid if the file cannot be read.

If you update the specfile subsequently while the program is running, flexi_logger re-reads it automatically and adapts its behavior according to the new content. If the file cannot be read anymore, e.g. because the format is not correct, the previous logspec remains active. If the file is corrected subsequently, the log spec update will work again.

Auto Trait Implementations

impl Send for Logger

impl Sync for Logger

Blanket Implementations

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

impl<T> From<T> for T[src]

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

type Error = Infallible

The type returned in the event of a conversion error.

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

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

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

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

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