emabee / flexi_logger

A flexible logger for rust programs that can write to stderr, stdout, and/or to log files
Apache License 2.0
307 stars 50 forks source link

Logging for different logging levels #169

Closed threeh11 closed 1 month ago

threeh11 commented 1 month ago

Hi! Thanks for your library. The question is whether it is possible to create two instances logging different levels and having different paths. Or there can't be more than 1 instance at all. I'm sorry if my question may seem silly. I'm just learning all this) Example:

Logger::try_with_str("info")?
    .log_to_file(
        FileSpec::default()
            .directory(self.path)
            .basename("path1")
            .use_timestamp(false)
            .suffix("log")
    )
    .write_mode(WriteMode::Direct)
    .format(detailed_format)
    .duplicate_to_stderr(Duplicate::Error)
    .start()?;
Logger::try_with_str("error")?
    .log_to_file(
        FileSpec::default()
            .directory(self.path)
            .basename("path2")
            .use_timestamp(false)
            .suffix("log")
    )
    .write_mode(WriteMode::Direct)
    .format(detailed_format)
    .duplicate_to_stderr(Duplicate::Error)
    .start()?;
emabee commented 1 month ago

flexi_logger is one of several alternative backends for the log crate. The log crate defines some macros that applications and libraries use in their code to write log entries. By registering a log backend in its main method, the application finally defines if and how these write calls will be handled. Registering two or more backends is not possible. flexi_logger offers instead some flexibility to handle various backends in parallel (stdout stderr, files, syslog, other writers).

See module writers for details how to direct individual log calls to explicitly registered writers.