Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File rotate doesn't delete old logs #181

Closed
jb-alvarado opened this issue Oct 27, 2024 · 1 comment
Closed

File rotate doesn't delete old logs #181

jb-alvarado opened this issue Oct 27, 2024 · 1 comment

Comments

@jb-alvarado
Copy link
Contributor

Hello,

in this code log files get not deleted:

use log::*;
use std::io::Write;

use flexi_logger::writers::{FileLogWriter, LogWriter};
use flexi_logger::{Age, Cleanup, Criterion, DeferredNow, FileSpec, Logger, Naming};

pub fn file_logger() -> Box<dyn LogWriter> {
    Box::new(
        FileLogWriter::builder(
            FileSpec::default()
                .suppress_timestamp()
                .directory("./logs")
                .discriminant("1")
                .basename("ffplayout"),
        )
        .append()
        .format(file_formatter)
        .rotate(
            Criterion::Age(Age::Day),
            Naming::TimestampsCustomFormat {
                current_infix: Some(""),
                format: "%Y-%m-%d",
            },
            Cleanup::KeepLogFiles(4),
        )
        .print_message()
        .try_build()
        .unwrap(),
    )
}

fn file_formatter(
    w: &mut dyn Write,
    now: &mut DeferredNow,
    record: &Record,
) -> std::io::Result<()> {
    write!(
        w,
        "[{}] [{:>5}] {}",
        now.now().format("%Y-%m-%d %H:%M:%S%.6f"),
        record.level(),
        record.args()
    )
}

fn main() {
    Logger::try_with_str("WARN")
        .expect("LogSpecification String has errors")
        .print_message()
        .log_to_stderr()
        .add_writer("Alert", file_logger())
        .start()
        .unwrap();

    error!(target : "{Alert,_Default}", "This is error message");
    warn!(target : "{Alert,_Default}", "This is a warning");
    info!(target : "{Alert,_Default}", "This is an info message");
    debug!(target : "{Alert,_Default}", "This is an debug message");
    trace!(target : "{Alert,_Default}", "This is an trace message");
}

Is there something wrong in my code, or is this a bug?

@jb-alvarado jb-alvarado changed the title File rotate doesn't delete old logs. File rotate doesn't delete old logs Oct 27, 2024
@emabee
Copy link
Owner

emabee commented Oct 29, 2024

Hi, your code is fine (except that you should keep the LoggerHandle alive (let _logger = Logger::try_with_...).
flexi_logger had indeed an issue that is now resolved with 0.29.5. I added also some docu lines at Naming::TimestampsCustomFormat to raise awareness that this variant has its risks (which you avoided).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants