[][src]Module flexi_logger::writers

A trait for extending flexi_logger with additional log writers, and two concrete implementations for writing to files or to the syslog.

(See LogWriter, FileLogWriter, and SyslogWriter for details).

Additional log writers can be used for sending log messages to additional log ouput streams than stderr or the default log file, as for example to an alert file or the syslog.

You can also use your own implementations of LogWriter.

You register each additional log writer with Logger.add_writer() under a target name. The target name is used subsequently in calls to the log macro for directing log messages to the desired writers.

A log call with a target value that has the form {Name1,Name2,...}, i.e., a comma-separated list of target names, within braces, is not sent to the default logger, but to the loggers specified explicitly in the list. In such a list you can again specify the default logger with the target name _Default.

In the following example we define an alert writer, and a macro to facilitate using it (and avoid using the explicit target specification in the macro call), and show some example calls.

use log::*;

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

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

// Define a macro for writing messages to the alert log and to the normal log
#[macro_use]
mod macros {
    #[macro_export]
    macro_rules! alert_error {
        ($($arg:tt)*) => (
            error!(target: "{Alert,_Default}", $($arg)*);
        )
    }
}

fn main() {
    Logger::with_env_or_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 the explicit macro
    alert_error!("This is another alert and log message");

    // Standard log macros write only to the normal log
    error!("This is a normal error 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 configurable LogWriter implementation that writes to a file or a sequence of files.

FileLogWriterBuilder

Builder for FileLogWriter.

SyslogWriter

An experimental configurable LogWriter implementation that writes log messages to the syslog (see RFC 5424).

Enums

SyslogConnector

Helper struct that connects to the syslog and implements Write.

SyslogFacility

Syslog Facility.

SyslogSeverity

SyslogConnector Severity.

Traits

LogWriter

Writes to a single log output stream.

Type Definitions

LevelToSyslogSeverity

Signature for a custom mapping function that maps the rust log levels to values of the syslog Severity.