Skip to content

Commit

Permalink
Merge pull request #3 from stackhpc/feat/log-default-extra
Browse files Browse the repository at this point in the history
Add logging of formatted extra params to default formatter
  • Loading branch information
mkjpryor authored Jul 18, 2024
2 parents 80fb5ac + c138741 commit 2f656c1
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion configomatic/logging.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import json
import logging

from pydantic import BaseModel, Field, field_validator

from .utils import merge


# Get the reserved keys for a log record
LOG_RECORD_RESERVED_KEYS = set(logging.LogRecord("_", 0, "_", 0, None, None, None).__dict__.keys())


class DefaultFormatter(logging.Formatter):
"""
Default logging formatter for this operator.
"""
def format(self, record):
# Calculate the extra parameters on the log record
extra = {k: v for k, v in record.__dict__.items() if k not in LOG_RECORD_RESERVED_KEYS}
# Add a quoted message property to the log record
record.__dict__["quotedmessage"] = json.dumps(record.getMessage())
# Add a formatted extra property to the log record
record.__dict__["formattedextra"] = " ".join(
k + "=" + json.dumps(str(v))
for k, v in extra.items()
)
return super().format(record)


class LessThanLevelFilter(logging.Filter):
def __init__(self, level):
if isinstance(level, int):
Expand Down Expand Up @@ -33,7 +55,8 @@ def default_formatters(cls, v):
return merge(
{
"default": {
"format": "[%(asctime)s] %(name)-20.20s [%(levelname)-8.8s] %(message)s",
"()": f"{__name__}.DefaultFormatter",
"format": "[%(asctime)s] %(name)s [%(levelname)-8.8s] %(quotedmessage)s %(formattedextra)s",
},
},
v or {}
Expand Down

0 comments on commit 2f656c1

Please sign in to comment.