Skip to content

Commit

Permalink
Merge pull request #1149 from nolar/jsonlogger
Browse files Browse the repository at this point in the history
Ensure compatibility with python-json-logger>=3.1.0
  • Loading branch information
nolar authored Dec 13, 2024
2 parents 4499b72 + 9a02704 commit 7f50469
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions kopf/_core/actions/loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@
import logging
from typing import Any, Dict, MutableMapping, Optional, Tuple

import pythonjsonlogger.jsonlogger
# Luckily, we do not mock these ones in tests, so we can import them into our namespace.
try:
# python-json-logger>=3.1.0
from pythonjsonlogger.core import RESERVED_ATTRS as _pjl_RESERVED_ATTRS
from pythonjsonlogger.json import JsonFormatter as _pjl_JsonFormatter
except ImportError:
# python-json-logger<3.1.0
from pythonjsonlogger.jsonlogger import JsonFormatter as _pjl_JsonFormatter # type: ignore
from pythonjsonlogger.jsonlogger import RESERVED_ATTRS as _pjl_RESERVED_ATTRS # type: ignore

from kopf._cogs.configs import configuration
from kopf._cogs.helpers import typedefs
Expand All @@ -40,20 +48,20 @@ class ObjectTextFormatter(ObjectFormatter, logging.Formatter):
pass


class ObjectJsonFormatter(ObjectFormatter, pythonjsonlogger.jsonlogger.JsonFormatter):
class ObjectJsonFormatter(ObjectFormatter, _pjl_JsonFormatter):
def __init__(
self,
*args: Any,
refkey: Optional[str] = None,
**kwargs: Any,
) -> None:
# Avoid type checking, as the args are not in the parent consructor.
reserved_attrs = kwargs.pop('reserved_attrs', pythonjsonlogger.jsonlogger.RESERVED_ATTRS)
reserved_attrs = kwargs.pop('reserved_attrs', _pjl_RESERVED_ATTRS)
reserved_attrs = set(reserved_attrs)
reserved_attrs |= {'k8s_skip', 'k8s_ref', 'settings'}
kwargs.update(reserved_attrs=reserved_attrs)
kwargs.setdefault('timestamp', True)
super().__init__(*args, **kwargs) # type: ignore # for untyped JsonFormatter.__init__()
super().__init__(*args, **kwargs)
self._refkey: str = refkey or DEFAULT_JSON_REFKEY

def add_fields(
Expand Down

0 comments on commit 7f50469

Please sign in to comment.