Skip to content
This repository was archived by the owner on Sep 14, 2020. It is now read-only.

Settings (official and documented) #336

Merged
merged 11 commits into from
Mar 27, 2020
Merged
Prev Previous commit
Next Next commit
Switch logging and posting from configs to settings
  • Loading branch information
nolar committed Mar 26, 2020
commit 1940afafe74f00725de6859d8394212e18ae2de0
3 changes: 1 addition & 2 deletions kopf/engines/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import logging
from typing import Tuple, MutableMapping, Any, Optional

from kopf import config
from kopf.engines import posting
from kopf.structs import bodies
from kopf.structs import configuration
Expand Down Expand Up @@ -45,7 +44,7 @@ def filter(self, record: logging.LogRecord) -> bool:
# Otherwise, we have nothing to post, and nothing to do.
settings: Optional[configuration.OperatorSettings]
settings = getattr(record, 'settings', None)
level_ok = record.levelno >= config.EventsConfig.events_loglevel
level_ok = settings is not None and record.levelno >= settings.posting.level
has_ref = hasattr(record, 'k8s_ref')
skipped = hasattr(record, 'k8s_skip') and getattr(record, 'k8s_skip')
return level_ok and has_ref and not skipped and super().filter(record)
Expand Down
11 changes: 7 additions & 4 deletions kopf/engines/posting.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
This also includes all logging messages posted by the framework itself.
"""
import asyncio
import logging
import sys
from contextvars import ContextVar
from typing import NamedTuple, NoReturn, Optional, Union, Iterator, Iterable, cast, TYPE_CHECKING

from kopf import config
from kopf.clients import events
from kopf.structs import bodies
from kopf.structs import configuration
Expand Down Expand Up @@ -102,7 +102,8 @@ def info(
reason: str,
message: str = '',
) -> None:
if config.EventsConfig.events_loglevel <= config.LOGLEVEL_INFO:
settings: configuration.OperatorSettings = settings_var.get()
if settings.posting.level <= logging.INFO:
event(obj, type='Normal', reason=reason, message=message)


Expand All @@ -112,7 +113,8 @@ def warn(
reason: str,
message: str = '',
) -> None:
if config.EventsConfig.events_loglevel <= config.LOGLEVEL_WARNING:
settings: configuration.OperatorSettings = settings_var.get()
if settings.posting.level <= logging.WARNING:
event(obj, type='Warning', reason=reason, message=message)


Expand All @@ -127,7 +129,8 @@ def exception(
_, exc, _ = sys.exc_info()
reason = reason if reason else type(exc).__name__
message = f'{message} {exc}' if message and exc else f'{exc}' if exc else f'{message}'
if config.EventsConfig.events_loglevel <= config.LOGLEVEL_ERROR:
settings: configuration.OperatorSettings = settings_var.get()
if settings.posting.level <= logging.ERROR:
event(obj, type='Error', reason=reason, message=message)


Expand Down
24 changes: 23 additions & 1 deletion kopf/structs/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,29 @@
"""
import dataclasses

from kopf import config # for legacy defaults only


@dataclasses.dataclass
class OperatorSettings:
class LoggingSettings:
pass


@dataclasses.dataclass
class PostingSettings:

level: int = dataclasses.field(
default_factory=lambda: config.EventsConfig.events_loglevel)
"""
A minimal level of logging events that will be posted as K8s Events.
The default is ``logging.INFO`` (i.e. all info, warning, errors are posted).

This also affects ``kopf.event()`` and similar functions
(``kopf.info()``, ``kopf.warn()``, ``kopf.exception()``).
"""


@dataclasses.dataclass
class OperatorSettings:
logging: LoggingSettings = dataclasses.field(default_factory=LoggingSettings)
posting: PostingSettings = dataclasses.field(default_factory=PostingSettings)
3 changes: 3 additions & 0 deletions tests/settings/test_defaults.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import logging

import kopf


async def test_declared_public_interface_and_promised_defaults():
settings = kopf.OperatorSettings()
assert settings.posting.level == logging.INFO