utils: add method for setting rollover info

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2021-01-06 09:10:01 -05:00
parent 8a18fa6c2f
commit 30637011e6
1 changed files with 14 additions and 6 deletions

View File

@ -28,19 +28,26 @@ class LocalQueueHandler(logging.handlers.QueueHandler):
except Exception: except Exception:
self.handleError(record) self.handleError(record)
# Timed Rotating File Handler, based on Klipper's implementation
class MoonrakerLoggingHandler(logging.handlers.TimedRotatingFileHandler): class MoonrakerLoggingHandler(logging.handlers.TimedRotatingFileHandler):
def __init__(self, software_version, filename, **kwargs): def __init__(self, software_version, filename, **kwargs):
super(MoonrakerLoggingHandler, self).__init__(filename, **kwargs) super(MoonrakerLoggingHandler, self).__init__(filename, **kwargs)
self.header = "Moonraker Log Start...\n" self.rollover_info = {
self.header += f"Git Version: {software_version}\n" 'header': f"{'-'*20}Moonraker Log Start{'-'*20}",
self.header += "="*80 + "\n" 'version': f"Git Version: {software_version}",
}
lines = [line for line in self.rollover_info.values() if line]
if self.stream is not None: if self.stream is not None:
self.stream.write(self.header) self.stream.write("\n".join(lines) + "\n")
def set_rollover_info(self, name, item):
self.rollover_info[name] = item
def doRollover(self): def doRollover(self):
super(MoonrakerLoggingHandler, self).doRollover() super(MoonrakerLoggingHandler, self).doRollover()
lines = [line for line in self.rollover_info.values() if line]
if self.stream is not None: if self.stream is not None:
self.stream.write(self.header) self.stream.write("\n".join(lines) + "\n")
# Parse the git version from the command line. This code # Parse the git version from the command line. This code
# is borrowed from Klipper. # is borrowed from Klipper.
@ -78,6 +85,7 @@ def setup_logging(log_file, software_version):
stdout_fmt = logging.Formatter( stdout_fmt = logging.Formatter(
'[%(filename)s:%(funcName)s()] - %(message)s') '[%(filename)s:%(funcName)s()] - %(message)s')
stdout_hdlr.setFormatter(stdout_fmt) stdout_hdlr.setFormatter(stdout_fmt)
file_hdlr = None
if log_file: if log_file:
file_hdlr = MoonrakerLoggingHandler( file_hdlr = MoonrakerLoggingHandler(
software_version, log_file, when='midnight', backupCount=2) software_version, log_file, when='midnight', backupCount=2)
@ -90,4 +98,4 @@ def setup_logging(log_file, software_version):
listener = logging.handlers.QueueListener( listener = logging.handlers.QueueListener(
queue, stdout_hdlr) queue, stdout_hdlr)
listener.start() listener.start()
return listener return listener, file_hdlr