paperless-ngx/src/documents/loggers.py

33 lines
843 B
Python
Raw Normal View History

2016-02-27 20:18:50 +00:00
import logging
import uuid
2016-02-27 20:18:50 +00:00
2020-11-02 18:54:27 +01:00
class PaperlessHandler(logging.Handler):
2016-02-27 20:18:50 +00:00
def emit(self, record):
# We have to do the import here or Django will barf when it tries to
# load this because the apps aren't loaded at that point
from .models import Log
kwargs = {"message": record.msg, "level": record.levelno}
2016-02-27 20:18:50 +00:00
if hasattr(record, "group"):
kwargs["group"] = record.group
Log.objects.create(**kwargs)
class LoggingMixin:
logging_group = None
def renew_logging_group(self):
self.logging_group = uuid.uuid4()
def log(self, level, message):
target = ".".join([self.__class__.__module__, self.__class__.__name__])
logger = logging.getLogger(target)
getattr(logger, level)(message, extra={
"group": self.logging_group
})