Last active
November 10, 2020 06:16
-
-
Save shirou/5f46426c1a0bb5d0d690ff745c08c3f7 to your computer and use it in GitHub Desktop.
Write file with timebased rotate and compression.
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
import file_logging | |
import time | |
file_logger = file_logging.setup("/tmp/foo/bar/example.log") | |
for i in range(0, 1000): | |
file_logger.info(f"msg: {i}") | |
time.sleep(0.3) |
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
import gzip | |
import logging | |
import os | |
import shutil | |
from logging.handlers import TimedRotatingFileHandler | |
def get_file_logger(filename: str, backupCount=14, utc=True): | |
ensure_dir(filename) | |
logger = logging.getLogger(name=filename) | |
logger.propagate = False # in order to hide from other logger | |
tr = TimedRotatingFileHandler( | |
filename=filename, when="midnight", backupCount=backupCount, utc=utc | |
) | |
tr.namer = namer | |
tr.rotator = rotator | |
logger.setLevel("INFO") | |
logger.addHandler(tr) | |
return logger | |
def ensure_dir(filename: str): | |
dir_name = os.path.dirname(filename) | |
if not os.path.exists(dir_name): | |
os.makedirs(dir_name) | |
## see https://docs.python.org/3/howto/logging-cookbook.html#using-a-rotator-and-namer-to-customize-log-rotation-processing | |
def namer(name): | |
return name + ".gz" | |
def rotator(source, dest): | |
with open(source, "rb") as _f_in, gzip.GzipFile(dest, mode="wb") as _f_out: | |
shutil.copyfileobj(_f_in, _f_out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment