[actions] move LoggerAdapter from 'output' to 'actions'

This commit is contained in:
Mike Fährmann
2024-06-30 20:41:51 +02:00
parent f41a5065b2
commit f7a6401031
3 changed files with 38 additions and 40 deletions

View File

@@ -12,6 +12,7 @@ import re
import time
import logging
import operator
import functools
from . import util, exception
@@ -51,6 +52,39 @@ def parse(actionspec):
return actions
class LoggerAdapter():
def __init__(self, logger, job):
self.logger = logger
self.extra = job._logger_extra
self.actions = job._logger_actions
self.debug = functools.partial(self.log, logging.DEBUG)
self.info = functools.partial(self.log, logging.INFO)
self.warning = functools.partial(self.log, logging.WARNING)
self.error = functools.partial(self.log, logging.ERROR)
def log(self, level, msg, *args, **kwargs):
msg = str(msg)
if args:
msg = msg % args
actions = self.actions[level]
if actions:
args = self.extra.copy()
args["level"] = level
for cond, action in actions:
if cond(msg):
action(args)
level = args["level"]
if self.logger.isEnabledFor(level):
kwargs["extra"] = self.extra
self.logger._log(level, msg, (), **kwargs)
def _level_to_int(level):
try:
return logging._nameToLevel[level]