import logging
import sys
_claviron = sys.modules["claviron"]
_STANDARD = set(vars(logging.LogRecord("", 0, "", 0, "", (), None))) | {"message", "asctime"}
class LogHandler(logging.Handler):
def __init__(self, channel, level=logging.NOTSET):
super().__init__(level)
self._channel = _claviron._log_channel(channel)
if self._channel is None:
raise ValueError(f"Claviron log channel {channel!r} is not declared in logging.yaml")
def emit(self, record):
try:
fields = {
key: value for key, value in vars(record).items() if key not in _STANDARD
}
fields["function"] = record.funcName
fields["line"] = record.lineno
if record.exc_info and record.exc_info[1] is not None:
error = record.exc_info[1]
fields["exception.class"] = type(error).__qualname__
fields["exception.message"] = str(error)
fields["exception.trace"] = self.formatException(record.exc_info)
_claviron._log(
self._channel,
record.levelname,
record.getMessage(),
target=record.name,
fields=fields,
)
except Exception:
self.handleError(record)
def formatException(self, exc_info):
return logging.Formatter().formatException(exc_info)