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

Make RotatingFileHandler pad numbers with leading zeros #130256

Closed
bjhend opened this issue Feb 18, 2025 · 3 comments
Closed

Make RotatingFileHandler pad numbers with leading zeros #130256

bjhend opened this issue Feb 18, 2025 · 3 comments
Labels
type-feature A feature request or enhancement

Comments

@bjhend
Copy link

bjhend commented Feb 18, 2025

Feature or enhancement

Proposal:

logging.handlers.RotatingFileHandler appends a number to a log file when performing the rotation. These numbers do not contain any leading zeros even if RotatingFileHandler is configured to create 10 or more backups, i.e. with backupCount set to 10 or bigger.

Hence, the log files are not properly ordered when listed. For example, a section of the list might look like:

log.18
log.19
log.2
log.20
log.21

or even worse:

log.1
log.10
log.100
log.11
log.12

It would be nice if RotatingFileHandler would pad the numbers with leading zeros up to the number of digits in the init argument backupCount in order to create a chronologically sorted list of log files, such that the latter example would look like:

log.001
log.002
...
log.010
log.011
log.012
...
log.100

This would help when analyzing the log files, particularly when you do a manual binary search for messages in a big number of rotated log files.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

@bjhend bjhend added the type-feature A feature request or enhancement label Feb 18, 2025
@serhiy-storchaka
Copy link
Member

That's what namer is for.

@bjhend
Copy link
Author

bjhend commented Feb 18, 2025

Thanks for pointing me on the namer. It made it possible for me to create a solution for my problem.

However, I still think that the RotatingFileHandler should produce numbers with equal digit lengths for several reasons:

  • I expect many people would prefer proper sorting of the logs for further analysis and processing
  • Creating a namer for this requires unnecessary many string and regular expression operations, because the namer receives the name as a string. It would likely be more efficient to implement that within the RotatingFileHandler.
  • Creating special namers based on a fixed length number would be easier if they need to work with the numbers

@serhiy-storchaka
Copy link
Member

This would be a breaking change. log.1 will not be deleted by a version that generates log.001. Also, there will be an issue in your application if you change backupCount and log.01 will become log.001.

This is an application level issue, and it should be solved at the application level. There is a lever for this, use it. You can also override other methods if you need more control.

BTW, many file managers on Linux are smart enough to show log.2 before log.10.

@serhiy-storchaka serhiy-storchaka closed this as not planned Won't fix, can't repro, duplicate, stale Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
Development

No branches or pull requests

2 participants