Skip to content

Commit

Permalink
BE: logging format
Browse files Browse the repository at this point in the history
  • Loading branch information
andreipradan committed Dec 3, 2024
1 parent f620d47 commit 052c329
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions src/mainframe/clients/logs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import logging

import axiom_py
Expand All @@ -9,25 +8,16 @@
logging.basicConfig(format=FORMAT, level=logging.INFO)


def parse_value(value):
try:
json.dumps(value)
return value
except TypeError:
return ""


def parse_record(record: logging.LogRecord) -> dict:
record = {
field: parse_value(getattr(record, field, None)) for field in record.__dict__
}
record["message"] = f"[Mainframe] {record['message']}"
return record
class MainframeHandler(AxiomHandler):
def emit(self, record: logging.LogRecord) -> None:
record.message = record.msg % record.args
record.env = settings.ENV
super().emit(record)


def get_default_logger(name):
logger = logging.getLogger(name)
if settings.ENV in ("prod", "rpi") and not logger.handlers:
if settings.ENV in ("local",) and not logger.handlers:
client = axiom_py.Client()
logger.addHandler(AxiomHandler(client, "mainframe"))
logger.addHandler(MainframeHandler(client, "mainframe"))
return logger

0 comments on commit 052c329

Please sign in to comment.