Module flexi_logger::writers [] [src]

A trait for additional log writers, and a configurable concrete implementation for a log writer writing to a file or a series of files.

Additional log writers can be used to direct certain log messages to other log ouput streams than the default log file, as for example an alert file or the syslog.

See also Logger.add_writer().

In the following example we define an alert writer, and a macro to facilitate using it, and show some example calls.

extern crate flexi_logger;
#[macro_use]
extern crate log;

use flexi_logger::Logger;
use flexi_logger::writers::FileLogWriter;

pub fn alert_logger() -> Box<FileLogWriter> {
    Box::new(FileLogWriter::builder()
        .discriminant("Alert")
        .suffix("alerts")
        .print_message()
        .instantiate()
        .unwrap())
}

#[macro_use]
mod macros {
    #[macro_export]
    macro_rules! alert_error {
        ($($arg:tt)*) => (
            error!(target: "{Alert,_Default}", $($arg)*);
        )
    }
}

fn main() {
    // Write logs to the normal logging file, and alerts to a separate file
    Logger::with_str("info")
        .print_message()
        .log_to_file()
        .add_writer("Alert", alert_logger())
        .start()
        .unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));

    // Explicitly send logs to different loggers
    error!(target : "{Alert}", "This is only an alert");
    error!(target : "{Alert,_Default}", "This is an alert and log message");

    // Nicer: use explicit macros
    alert_error!("This is another alert and log message");
    warn!("This is a warning");
    info!("This is an info message");
    debug!("This is a debug message - you will not see it");
    trace!("This is a trace message - you will not see it");
}

Structs

FileLogWriter

A LogWriter that writes to a file or, if rotation is used, a sequence of files.

FileLogWriterBuilder

Builder for FileLogWriter.

Traits

LogWriter

Writes to a single log output stream.