1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 02:32:33 +01:00

[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
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
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]

View File

@ -33,6 +33,7 @@ stdout_write = output.stdout_write
class Job():
"""Base class for Job types"""
ulog = None
_logger_adapter = output.LoggerAdapter
def __init__(self, extr, parent=None):
if isinstance(extr, str):
@ -77,9 +78,9 @@ class Job():
actions = extr.config("actions")
if actions:
from .actions import parse
from .actions import LoggerAdapter, parse
self._logger_adapter = LoggerAdapter
self._logger_actions = parse(actions)
self._wrap_logger = self._wrap_logger_actions
path_proxy = output.PathfmtProxy(self)
self._logger_extra = {
@ -267,10 +268,7 @@ class Job():
return self._wrap_logger(logging.getLogger(name))
def _wrap_logger(self, logger):
return output.LoggerAdapter(logger, self)
def _wrap_logger_actions(self, logger):
return output.LoggerAdapterActions(logger, self)
return self._logger_adapter(logger, self)
def _write_unsupported(self, url):
if self.ulog:

View File

@ -10,7 +10,6 @@ import os
import sys
import shutil
import logging
import functools
import unicodedata
from . import config, util, formatter
@ -92,39 +91,6 @@ class LoggerAdapter():
self.logger._log(logging.ERROR, msg, args, **kwargs)
class LoggerAdapterActions():
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)
class PathfmtProxy():
__slots__ = ("job",)