首页 > 解决方案 > 如何将日志记录写入现有的错误日志文件

问题描述

我的 django 错误写入的错误日志是高度可访问的。因此,在我的代码中,我想使用同一个文件进行日志记录。我的配置看起来像这样。

设置.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'ERROR',
            'class': 'logging.FileHandler',
            'filename': '/opt/python/log/djangoerrors.log'
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'ERROR',
            'propagate': True,
        },
    },
}

在我看来,我有这个:

import logging
logger = logging.getLogger(__name__)

def myview(request):
    ... some code ...
    logger.error('hello. This should be logged to the error file.')

不幸的是,这个文本没有被写入我的 djangoerrors.log 文件。你能看到我错过了什么吗?

谢谢!

标签: django

解决方案


推荐阅读