Skip to content

Commit

Permalink
chg: [logger] logging only generates one file now and rotates keeping…
Browse files Browse the repository at this point in the history
… one backup.
  • Loading branch information
dario-br committed Dec 19, 2024
1 parent d898f17 commit fe8c262
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/sysdiagnose/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ def main():
logger2file = None
for case_id in case_ids:
# Handle file logging
time_str = time.strftime("%Y%m%dT%H%M%S")
filename = f"{time_str}-log-parse.jsonl"
filename = f"log-parse.jsonl"
folder = sd.config.get_case_parsed_data_folder(case_id)
# https://stackoverflow.com/questions/13839554/how-to-change-filehandle-with-python-logging-on-the-fly-with-different-classes-a
if logger2file is None:
Expand Down Expand Up @@ -191,8 +190,7 @@ def main():
logger2file = None
for case_id in case_ids:
# Handle file logging
time_str = time.strftime("%Y%m%dT%H%M%S")
filename = f"{time_str}-log-analyse.jsonl"
filename = f"log-analyse.jsonl"
folder = sd.config.get_case_parsed_data_folder(case_id)
# https://stackoverflow.com/questions/13839554/how-to-change-filehandle-with-python-logging-on-the-fly-with-different-classes-a
if logger2file is None:
Expand Down
7 changes: 6 additions & 1 deletion src/sysdiagnose/utils/logger.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import logging
import logging.handlers
from pythonjsonlogger import jsonlogger
from datetime import datetime

# 3MB max
MAX_BYTES = 3*1024*1024
MAX_LOGFILES = 1

logger = logging.getLogger('sysdiagnose')
# By default, we want to have the possibility to log everything.
logger.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -43,7 +48,7 @@ def get_json_handler(filename: str, level: int = logging.DEBUG) -> logging.FileH
fmt='%(created)f %(asctime)s %(levelname)s %(module)s %(message)s',
rename_fields={'asctime': 'datetime', 'created': 'timestamp'})
# File handler
fh = logging.FileHandler(filename)
fh = logging.handlers.RotatingFileHandler(filename=filename, maxBytes=MAX_BYTES, backupCount=MAX_LOGFILES)
fh.setLevel(level)
fh.setFormatter(fmt_json)

Expand Down

0 comments on commit fe8c262

Please sign in to comment.