loghelper: add support for structured logging

Support logging to json structures, useful for log parsers
and analytics.  When enabled logging to stdout is still human
readable, so both forms of logging are available simultaneously.

Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan committed 2025-03-26 05:29:33 -04:00
1 parent ebaa6645b2
commit 1226a08176
2 files changed
+59 -3

No files matched your search

+50 -2
View File
@@ -14,6 +14,7 @@ import asyncio
import platform
from queue import SimpleQueue as Queue
from .common import RequestType
from .utils import json_wrapper as jsonw
# Annotation imports
from typing import (
@@ -30,6 +31,34 @@ if TYPE_CHECKING:
from .common import WebRequest
from .components.klippy_connection import KlippyConnection
class StructuredFormatter(logging.Formatter):
def format(self, record: logging.LogRecord) -> str:
record.message = record.getMessage()
record.asctime = self.formatTime(record, self.datefmt)
msg = record.message
if record.exc_info:
# Cache the traceback text to avoid converting it multiple times
# (it's constant anyway)
if not record.exc_text:
record.exc_text = self.formatException(record.exc_info)
if record.exc_text:
if msg[-1:] != "\n":
msg += "\n"
msg += record.exc_text
if record.stack_info:
if msg[-1:] != "\n":
msg += "\n"
msg += self.formatStack(record.stack_info)
data = {
"time": record.asctime,
"level": record.levelname,
"file": record.filename,
"function": record.funcName,
"line": record.lineno,
"message": msg.strip()
}
return jsonw.dumps(data).decode()
# Coroutine friendly QueueHandler courtesy of Martjin Pieters:
# https://www.zopatista.com/python/2019/05/11/asyncio-logging/
class LocalQueueHandler(logging.handlers.QueueHandler):
@@ -59,6 +88,9 @@ class MoonrakerLoggingHandler(logging.handlers.TimedRotatingFileHandler):
def write_header(self) -> None:
if self.stream is None:
return
if self.app_args["structured_logging"]:
self._write_structured_header()
return
strtime = time.asctime(time.gmtime())
header = f"{'-'*20} Log Start | {strtime} {'-'*20}\n"
self.stream.write(header)
@@ -69,6 +101,18 @@ class MoonrakerLoggingHandler(logging.handlers.TimedRotatingFileHandler):
lines = [line for line in self.rollover_info.values() if line]
self.stream.write("\n".join(lines) + "\n")
def _write_structured_header(self) -> None:
msg_parts = [f"platform: {platform.platform(terse=True)}"]
msg_parts.extend([f"{k}: {v}" for k, v in self.app_args.items()])
if self.rollover_info:
msg_parts.extend([line for line in self.rollover_info.values() if line])
msg = "\n".join(msg_parts)
record = logging.LogRecord(
"root", logging.INFO, __file__, 93, msg, None, None,
func="write_header"
)
self.emit(record)
class LogManager:
def __init__(
self, app_args: Dict[str, Any], startup_warnings: List[str]
@@ -94,8 +138,12 @@ class LogManager:
try:
self.file_hdlr = MoonrakerLoggingHandler(
app_args, when='midnight', backupCount=2)
formatter = logging.Formatter(
'%(asctime)s [%(filename)s:%(funcName)s()] - %(message)s')
if app_args["structured_logging"]:
formatter: logging.Formatter = StructuredFormatter()
else:
formatter = logging.Formatter(
'%(asctime)s [%(filename)s:%(funcName)s()] - %(message)s'
)
self.file_hdlr.setFormatter(formatter)
self.listener = logging.handlers.QueueListener(
queue, self.file_hdlr, stdout_hdlr)
+9 -1
View File
@@ -626,6 +626,13 @@ def main(from_package: bool = True) -> None:
default=get_env_bool("MOONRAKER_DISABLE_FILE_LOG"),
help="disable logging to a file"
)
parser.add_argument(
"-s", "--structured-logging",
action='store_const',
const=True,
default=get_env_bool("MOONRAKER_STRUCTURED_LOGGING"),
help="Enable structured file logging"
)
parser.add_argument(
"-v", "--verbose",
action='store_const',
@@ -691,7 +698,8 @@ def main(from_package: bool = True) -> None:
"is_backup_config": False,
"is_python_package": from_package,
"instance_uuid": instance_uuid,
"unix_socket_path": unix_sock
"unix_socket_path": unix_sock,
"structured_logging": cmd_line_args.structured_logging
}
# Setup Logging