Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure compatibility with python-json-logger>=3.1.0 #1149

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading