From 143db0d1e2a14578b24e87e23f89e13762d9843d Mon Sep 17 00:00:00 2001 From: Rogerio Goncalves Date: Sat, 30 Mar 2024 07:58:10 -0400 Subject: [PATCH] core: rotate log file at every restart (#181) --- README.md | 2 ++ klippy/klippy.py | 15 ++++++++++++++- klippy/queuelogger.py | 20 ++++++++++++++------ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 923bbb6d3..5277c3a41 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,8 @@ If I want my printer to light itself on fire, I should be able to make my printe - [core: danger_options](https://github.com/DangerKlippers/danger-klipper/pull/67) +- [core: rotate log file at every restart](https://github.com/DangerKlippers/danger-klipper/pull/181) + - [fan: normalising Fan PWM power](https://github.com/DangerKlippers/danger-klipper/pull/44) ([klipper#6307](https://github.com/Klipper3d/klipper/pull/6307)) - [fan: reverse FAN](https://github.com/DangerKlippers/danger-klipper/pull/51) ([klipper#4983](https://github.com/Klipper3d/klipper/pull/4983)) diff --git a/klippy/klippy.py b/klippy/klippy.py index 3bee3b018..1eb8ba47a 100644 --- a/klippy/klippy.py +++ b/klippy/klippy.py @@ -436,6 +436,11 @@ def main(): dest="logfile", help="write log to file instead of stderr", ) + opts.add_option( + "--rotate-log-at-restart", + action="store_true", + help="rotate the log file at every restart", + ) opts.add_option( "-v", action="store_true", dest="verbose", help="enable debug messages" ) @@ -485,7 +490,13 @@ def main(): bglogger = None if options.logfile: start_args["log_file"] = options.logfile - bglogger = queuelogger.setup_bg_logging(options.logfile, debuglevel) + bglogger = queuelogger.setup_bg_logging( + filename=options.logfile, + debuglevel=debuglevel, + rotate_log_at_restart=options.rotate_log_at_restart, + ) + if options.rotate_log_at_restart: + bglogger.doRollover() else: logging.getLogger().setLevel(debuglevel) logging.info("=======================") @@ -532,6 +543,8 @@ def main(): main_reactor = printer = None logging.info("Restarting printer") start_args["start_reason"] = res + if options.rotate_log_at_restart and bglogger is not None: + bglogger.doRollover() if bglogger is not None: bglogger.stop() diff --git a/klippy/queuelogger.py b/klippy/queuelogger.py index 8b980238e..7716b92ef 100644 --- a/klippy/queuelogger.py +++ b/klippy/queuelogger.py @@ -5,6 +5,7 @@ # This file may be distributed under the terms of the GNU GPLv3 license. import logging, logging.handlers, threading, queue, time + # Class to forward all messages through a queue to a background thread class QueueHandler(logging.Handler): def __init__(self, queue): @@ -24,10 +25,15 @@ def emit(self, record): # Class to poll a queue in a background thread and log each message class QueueListener(logging.handlers.TimedRotatingFileHandler): - def __init__(self, filename): - logging.handlers.TimedRotatingFileHandler.__init__( - self, filename, when="midnight", backupCount=5 - ) + def __init__(self, filename, rotate_log_at_restart): + if rotate_log_at_restart: + logging.handlers.TimedRotatingFileHandler.__init__( + self, filename, when="S", interval=60 * 60 * 24, backupCount=5 + ) + else: + logging.handlers.TimedRotatingFileHandler.__init__( + self, filename, when="midnight", backupCount=5 + ) self.bg_queue = queue.Queue() self.bg_thread = threading.Thread(target=self._bg_thread) self.bg_thread.start() @@ -72,9 +78,11 @@ def doRollover(self): MainQueueHandler = None -def setup_bg_logging(filename, debuglevel): +def setup_bg_logging(filename, debuglevel, rotate_log_at_restart): global MainQueueHandler - ql = QueueListener(filename) + ql = QueueListener( + filename=filename, rotate_log_at_restart=rotate_log_at_restart + ) MainQueueHandler = QueueHandler(ql.bg_queue) root = logging.getLogger() root.addHandler(MainQueueHandler)