From 3ad28f684b0549ce5297bbfcddb9ce354a3f9660 Mon Sep 17 00:00:00 2001 From: Matthias Veit Date: Mon, 2 Oct 2023 21:02:29 +0200 Subject: [PATCH] [fix] Allow overriding the json format (#15) --- fixcloudutils/logging/__init__.py | 22 +++++++++++----------- fixcloudutils/logging/json_logger.py | 12 ++++++++---- pyproject.toml | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/fixcloudutils/logging/__init__.py b/fixcloudutils/logging/__init__.py index f11c6ae..96bb218 100644 --- a/fixcloudutils/logging/__init__.py +++ b/fixcloudutils/logging/__init__.py @@ -22,7 +22,7 @@ import logging import os from logging import StreamHandler, basicConfig -from typing import Optional, List +from typing import Optional, List, Dict from .json_logger import JsonFormatter from .prometheus_counter import PrometheusLoggingCounter @@ -38,22 +38,22 @@ def setup_logger( json_format: bool = True, count_logs: bool = True, log_format: Optional[str] = None, + json_format_dict: Optional[Dict[str, str]] = None, ) -> List[StreamHandler]: # type: ignore log_level = level or logging.INFO # override log output via env var plain_text = os.environ.get("LOG_TEXT", "false").lower() == "true" handler = PrometheusLoggingCounter(component) if count_logs else StreamHandler() if json_format and not plain_text: - formatter = JsonFormatter( - { - "timestamp": "asctime", - "level": "levelname", - "message": "message", - "pid": "process", - "thread": "threadName", - }, - static_values={"component": component}, - ) + format_dict = json_format_dict or { + "level": "levelname", + "timestamp": "asctime", + "message": "message", + "logger": "name", + "pid": "process", + "thread": "threadName", + } + formatter = JsonFormatter(format_dict, static_values={"component": component}) handler.setFormatter(formatter) basicConfig(handlers=[handler], force=force, level=log_level) else: diff --git a/fixcloudutils/logging/json_logger.py b/fixcloudutils/logging/json_logger.py index 6c8b120..5634373 100644 --- a/fixcloudutils/logging/json_logger.py +++ b/fixcloudutils/logging/json_logger.py @@ -45,11 +45,15 @@ def usesTime(self) -> bool: # noqa: N802 return self.__uses_time def format(self, record: LogRecord) -> str: - record.message = record.getMessage() - if self.__uses_time: - record.asctime = self.formatTime(record, self.time_format) + def prop(name: str) -> str: + if name == "asctime": + return self.formatTime(record, self.time_format) + elif name == "message": + return record.getMessage() + else: + return getattr(record, name, "n/a") - message_dict = {fmt_key: record.__dict__[fmt_val] for fmt_key, fmt_val in self.fmt_dict.items()} + message_dict = {fmt_key: prop(fmt_val) for fmt_key, fmt_val in self.fmt_dict.items()} message_dict.update(self.static_values) if record.exc_info: if not record.exc_text: diff --git a/pyproject.toml b/pyproject.toml index af2f07a..b97c260 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "fixcloudutils" -version = "1.7.0" +version = "1.7.1" authors = [{ name = "Some Engineering Inc." }] description = "Utilities for fixcloud." license = { file = "LICENSE" }