0

我正在尝试记录(默认情况下)usernameproject(可以从request对象决定)。我不想context手动添加到每个日志。

问题是我无法Django添加request或直接添加username到. 我尝试了几十种方法。projectLogRecord

这是我的代码:

中间件.py

import threading
local = threading.local()

class LoggingRequestMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.

    def __call__(self, request):
        # Code to be executed for each request before
        # the view (and later middleware) are called.
        setattr(local, 'request', request)
        response = self.get_response(request)

        # Code to be executed for each request/response after
        # the view is called.

        return response

设置.py

def add_username_to_log(record):
local = threading.local()
record.username = '-'
request = getattr(local,'request',None)
print(request)

return True

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': LOGGING_VERBOSE_FORMAT,
            'style': '{',
        },
    },
    'filters': {
        'context_filter': {
            '()': 'django.utils.log.CallbackFilter',
            'callback': add_username_to_log,
        },

    },
    'handlers': {
        'console': {
            'level': DEFAULT_LOG_LEVEL,
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
            'filters': ['context_filter'],
        },
        'file_main': {
            'level': DEFAULT_LOG_LEVEL,
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(LOG_PATH, 'main.log'),
            'maxBytes': DEFAULT_LOG_SIZE,
            'formatter': 'verbose',
            'filters': ['context_filter'],
            'backupCount': 0,
        },

    },
    'loggers': {
        '': {
            'handlers': ['file_main'],
            'level': DEFAULT_LOG_LEVEL,
            'propagate': False,
        },

    },
}

request对象始终是None。你知道为什么吗?

4

1 回答 1

1

threading.local()每次返回一个新对象,您必须读取和写入同一个对象。

locals_a = threading.local()
locals_a.foo = 1
hasattr(locals_a, 'foo')  # True
locals_b = threading.local()
hasattr(locals_b, 'foo')  # False

您需要在 1 个地方定义您的 locals 对象,然后您可以在每次需要访问请求并读取和写入该对象的任何地方导入该对象。作为一个基本示例,这应该有效

def add_username_to_log(record):
    from middleware import local
    request = getattr(local,'request',None)
于 2020-04-26T10:34:51.200 回答