-
Notifications
You must be signed in to change notification settings - Fork 20
Add a log rotation function to the file handler #56
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
Conversation
We are not ignoring this, but this adds quite a bit of code to a simple library, and we need to check the consequences of doing that: does it break some existing examples, etc.? |
Thanks for the review. I considered making this a separate library that monitored log sizes. I decided to modify this library instead because:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding this functionality @ilikecake!
I tested the existing examples and confirmed that this PR does not cause any issues with them.
Instead of implementing this functionality inside of FileHandler
class directly I think it would be best to create RotatingFileHandler
as a subclass of FileHandler
and have it be a strict subset of the CPython API and functionality: https://docs.python.org/3/library/logging.handlers.html#rotatingfilehandler
We don't necessarily need to implement 100% percent of it's functionality, but any thing we do implement should match the CPython API to whatever extent is possible.
So RotatingFileHandler would have the argument named maxBytes
instead of LogFileSizeLimit
and we would leave out the OldFilePostfix
argument and behavior. But we could still create another subclass of RotatingFileHandler like CustomNameRotatingFileHandler or something which does have the OldFilePostfix
argument and behavior. Even though the CPython API doesn't have that option that I can find, it does seem like nice functionality to me, but we should just move it so that it doesn't break CPython compatibility.
Update the code to make a subclass of filehandler that implements some of the RotatingFileHandler from: https://docs.python.org/3/library/logging.handlers.html#rotatingfilehandler
Remove some debug statements
Thanks for the review. I have updated the code per your suggestion. I have done some tests with my hardware and it seems to be working correctly. Is there a way to generate the doc files locally so that I can make sure the formatting is right? I try the procedure from here, but it gives me an error.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ilikecake thanks for implementing those changes. This is great functionality to have for this library!
These changes are looking good to me now. I tested successfully with the existing FileHandler example and a modified one to use the RotatingFileHandler on a Memeento 9.0.4.
With regards to the docs build: Currently the docs build infrastructure does not support python 3.12. You found the right process, but it's not currently compatible with that version of python. For now you'll need to use python 3.11 or lower in order to build the docs locally.
The typing_extensions
module is different now so this: from typing_extensions import Protocol
is raising an exception which then results in the WriteableStream protocol class not being created.
I think that if pylint is bumped to 3.1.0 in |
Updating https://github.com/adafruit/Adafruit_CircuitPython_Logging to 5.3.0 from 5.2.5: > Merge pull request adafruit/Adafruit_CircuitPython_Logging#56 from ilikecake/filehandler-roll-logs Updating https://github.com/adafruit/Adafruit_CircuitPython_Requests to 4.0.0 from 3.2.10: > Merge pull request adafruit/Adafruit_CircuitPython_Requests#193 from justmobilize/exception-type-updates Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA: > Updated download stats for the libraries
Adds a simple log rotation function to the file handler. This will keep the log files from growing without bound.
This update adds two optional variables to the
FileHandler
class.LogFileSizeLimit
: the size limit of the log file in bytes.OldFilePostfix
: What to append to the log filename to save the old log.If the file size limit is set, when the
emit
function is called, the size of the log file will be compared to the limit. If the file is larger than the limit, it will be renamed and a new log file will be started. The old log file is renamed by appendingOldFilePostfix
to the file name. If there is another file with this name, it will be deleted.If the file size limit is not set, the class acts the same as before this change.
I have tested this on an ESP32-S3 Feather running CircuitPython 9.0.3.