Skip to content

Commit

Permalink
[fix] Allow overriding the json format (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias authored Oct 2, 2023
1 parent 513456b commit 3ad28f6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
22 changes: 11 additions & 11 deletions fixcloudutils/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
12 changes: 8 additions & 4 deletions fixcloudutils/logging/json_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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" }
Expand Down

0 comments on commit 3ad28f6

Please sign in to comment.