forked from micropython/micropython-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logging: Introduced a supplemental logging rotating handler package
Co-authored-by: Paul Sokolovsky <pfalcon@users.sourceforge.net> Co-authored-by: Stefan Lehmann <stlm@posteo.de>
- Loading branch information
1 parent
d97b821
commit 0ee18ec
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Rotating File Logging Handler | ||
|
||
Add-on package for [`logging`](../logging/). Provides a Handler that can rotate | ||
logging output between multiple files while keeping the total filesize below a | ||
specified limit. |
65 changes: 65 additions & 0 deletions
65
python-stdlib/logging-rotating-handler/logging/rotating_handler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import os | ||
from . import Handler | ||
|
||
# Author: Stefan Lehmann | ||
# Source: https://github.com/pfalcon/pycopy-lib/blob/master/logging/logging/handlers.py | ||
|
||
def try_remove(fn: str) -> None: | ||
"""Try to remove a file if it existst.""" | ||
try: | ||
os.remove(fn) | ||
except OSError: | ||
pass | ||
|
||
|
||
def get_filesize(fn: str) -> int: | ||
"""Return size of a file.""" | ||
return os.stat(fn)[6] | ||
|
||
|
||
class RotatingFileHandler(Handler): | ||
"""A rotating file handler like RotatingFileHandler. | ||
Compatible with CPythons `logging.handlers.RotatingFileHandler` class. | ||
""" | ||
|
||
def __init__(self, filename, maxBytes=0, backupCount=0): | ||
super().__init__() | ||
self.filename = filename | ||
self.maxBytes = maxBytes | ||
self.backupCount = backupCount | ||
|
||
try: | ||
self._counter = get_filesize(self.filename) | ||
except OSError: | ||
self._counter = 0 | ||
|
||
def emit(self, record): | ||
"""Write to file.""" | ||
msg = self.formatter.format(record) | ||
s_len = len(msg) | ||
|
||
if self.maxBytes and self.backupCount and self._counter + s_len > self.maxBytes: | ||
# remove the last backup file if it is there | ||
try_remove(self.filename + ".{0}".format(self.backupCount)) | ||
|
||
for i in range(self.backupCount - 1, 0, -1): | ||
if i < self.backupCount: | ||
try: | ||
os.rename( | ||
self.filename + ".{0}".format(i), | ||
self.filename + ".{0}".format(i + 1), | ||
) | ||
except OSError: | ||
pass | ||
|
||
try: | ||
os.rename(self.filename, self.filename + ".1") | ||
except OSError: | ||
pass | ||
self._counter = 0 | ||
|
||
with open(self.filename, "a") as f: | ||
f.write(msg + "\n") | ||
|
||
self._counter += s_len |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
metadata(version="0.7.5", | ||
author="Stefan Lehmann, Paul Sokolovsky", | ||
description="Rotating file handler for the logging package") | ||
|
||
require("logging") | ||
|
||
package("logging") |