[−][src]Struct flexi_logger::Logger
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
-
specify your desired (initial) loglevel-specification
- either programmatically as a String
(
Logger::with_str()
) - or by providing a String in the environment
(
Logger::with_env()
), - or by combining both options
(
Logger::with_env_or_str()
), - or by building a
LogSpecification
programmatically (Logger::with()
),
- either programmatically as a String
(
-
use the desired configuration methods,
-
and finally start the logger with
Methods
impl Logger
[src]
Choose a way to 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]
Choose a way how to start logging.
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_reconfigurable(
self
) -> Result<ReconfigurationHandle, FlexiLoggerError>
[src]
self
) -> Result<ReconfigurationHandle, FlexiLoggerError>
please use start()
instead
This method is deprecated. The standard start()
method now returns a
reconfiguration handle because there is no performance penalty any more for
reconfigurability.
pub fn start_with_specfile<P: AsRef<Path>>(
self,
specfile: P
) -> Result<(), FlexiLoggerError>
[src]
self,
specfile: P
) -> Result<(), FlexiLoggerError>
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
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.
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.
The factory methods Logger::with_str()
, Logger::with_env()
,
and Logger::with_env_or_str()
,
parse a log specification String, and deduce from it a LogSpecification
object.
Parsing errors are reported to stdout, but effectively ignored; in worst case, a
LogSpecification might be used that turns off logging completely!
This method gives programmatic access to parse errors, if there were any.
In the following example we just panic if the spec was not free of errors:
Logger::with_str(some_log_spec_string) .check_parser_error() .unwrap() .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 the log entries, rather than formats::default_format.
You can either choose between some predefined variants,
default_format
, opt_format
, detailed_format
, with_thread
,
or you create and use your own format function
with the signature fn(&Record) -> String
.
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, rotate_over_size: usize, cleanup: Cleanup) -> Logger
[src]
Prevents indefinite growth of 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 or exceeds the specified file size, the file will be closed and a new file will be opened.
The rotate-over-size is given in bytes, e.g. rotate_over_size(1_000)
will rotate
files once they reach a size of 1000 bytes.
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
- if this file exceeds the specified rotate-over-size, it is closed and renamed to a file
with a sequential number infix,
and then the logging continues again to the (fresh) file with infix
_rCURRENT
Example:
After some logging with your program my_prog
, you will find files like
my_prog_r00000.log
my_prog_r00001.log
my_prog_r00002.log
my_prog_rCURRENT.log
The cleanup parameter allows defining the strategy for dealing with older files. See Cleanup for details.
pub fn rotate_over_size(self, rotate_over_size: usize) -> Logger
[src]
use rotate()
Prevents indefinite growth of log files.
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.
pub fn create_symlink<P: Into<PathBuf>>(self, symlink: P) -> Logger
[src]
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]
self,
name: S,
writer: Box<dyn LogWriter>
) -> Logger
Registers a LogWriter implementation under the given target name.
The target name should not start with an underscore.
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<(u64, 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_rotate_over_size(self, rotate_over_size: Option<usize>) -> Logger
[src]
use o_rotate()
This option only has an effect if log_to_file
is set to true.
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.
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.
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.
pub fn o_create_symlink<P: Into<PathBuf>>(self, symlink: Option<P>) -> Logger
[src]
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.
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,