Skip to content

tracing-appender: max_log_files deletes 1 more file than it should on startup #3496

Description

@vincenpt

Bug Report

Version

  • tracing = "0.1.44"
  • tracing-subscriber = { version = "0.3.22", features = ["env-filter", "json"] }
  • tracing-appender = "0.2.4"
  • tracing-core = "0.1.36"
  • tracing-test = "0.2.5"

Platform

Crates

  • tracing-appender = "0.2.4"

Description

The short description of my problem:
When using a RollingFileAppender with max_log_files set, the Inner struct's prune_old_logs will delete down to max_log_files - 1 log files.

This is a problem because if the log file we start writing to has the same name as an existing log file, we are left with 'max_log_files - 1' log files, unexpectedly losing log files that should have been preserved still.

The longer explanation for how this happens:

  1. Lets assume that max_log_files = 3
  2. The current date is March 20th, 2026
  3. The application has been running for a number of days and there are three log files being retained
    • application.log.2026-03-20
    • application.log.2026-03-19
    • application.log.2026-03-18
  4. The application is now restarted.
  5. On startup, pruno_old_logs will delete application.log.2026-03-18 in order to reduce the log count to max_log_files - 1
  6. The RollingFileAppender will then start writing to application.log.2026-03-20, leaving us with only 2 log files.

I think this behavior is in place under the assumption that a new log file will get created, so by deleting an extra log file the appender is leaving room for that new log file to be created. However, in the case of a quick restart (say, for an upgrade, or due to a crash) log data that is still relevant may be deleted.

I believe this line of code is where part of the issue occurs:

for (file, _) in files.iter().take(files.len() - (max_files - 1)) {

My workaround for now is going to be to artificially increase the number of log files that an end user has configured. If they say to retain 3 log files, I will retain 4. While this will work, I then foresee people writing defects for my code complaining that I am retaining more log data than they have configured my application to retain.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions