Loguru:如何混淆日志中的数据

Loguru: how to obfuscate data in logs

使用 python 记录器,我可以像这样混淆数据:

import logging
import re
import sys


class MySensitiveFormatter(logging.Formatter):
    """Formatter that removes sensitive information."""

    @staticmethod
    def _filter(s):
        """Remove credentials."""
        result = re.sub(r"pass: .*", "pass: xxx", s)
        return result

    def format(self, record):
        """Obfuscate sensitive information."""
        original = logging.Formatter.format(self, record)
        return self._filter(original)


stream_handler = logging.StreamHandler(stream=sys.stdout)
stream_handler.setFormatter(MySensitiveFormatter())
logger = logging.getLogger("demo")
logger.setLevel("INFO")
logger.addHandler(stream_handler)

logger.info("This is a demo and here is the pass: secret")

prints => 这是一个演示,这是通行证:xxx

loguru 中我无法添加 formatter/handler 并且过滤器删除了整个记录(这不是我想要的)。如何使用 loguru?

实现此目的

这是它的工作原理:

def obfuscate_message(message: str):
    """Obfuscate sensitive information."""
    result = re.sub(r"pass: .*", "pass: xxx", s)
    return result


class LoguruInterceptHandler(logging.Handler):
    """Enable loguru logging."""

    def emit(self, record):
        """Get corresponding Loguru level if it exists."""
        try:
            level = loguru.logger.level(record.levelname).name
        except ValueError:
            level = record.levelno

        # Find caller from where originated the logged message
        frame, depth = logging.currentframe(), 2
        while frame.f_code.co_filename == logging.__file__:
            frame = frame.f_back
            depth += 1

        # filter sensitive data (this does the trick)
        message = obfuscate_message(record.getMessage())

        loguru.logger.opt(depth=depth, exception=record.exc_info).log(level, message)

这样使用:

logger = logging.getLogger()
logger.handlers = [LoguruInterceptHandler()]  # overwrite old handlers
logger.info("This is a demo and here is the pass: secret")

这是loguru团队提供的正确方式:

def obfuscate_message(message: str):
    """Obfuscate sensitive information."""
    result = re.sub(r"pass: .*", "pass: xxx", s)
    return result

def formatter(record):
    record["extra"]["obfuscated_message"] = obfuscate_message(record["message"])
    return "[{level}] {extra[obfuscated_message]}\n{exception}"

logger.add(sys.stderr, format=formatter)

详情见loguru issue