Django logger.error 不将日志条目保存到 debug.log 文件中

Django logger.error does not save logging entry into the debug.log file

我按照 django 文档并尝试使用 logger.error 将错误保存到 debug.log 文件中。

LOGGING = {

'version': 1,
'disable_existing_loggers': False,
'handlers': {
    'file': {
        'level': 'DEBUG',
        'class': 'logging.FileHandler',
        'filename': 'debug.log',
    },
},
'loggers': {
    'django': {
        'handlers': ['file'],
        'level': 'DEBUG',
        'propagate': True,
    },
},
}


import logging
logger = logging.getLogger(__name__)
...... // some code here
    if not data:
        logger.error('Search term is not valid')

句子 "Search term is not valid" 确实在控制台中打印出来,但没有保存到 debug.log 文件中。

我不太清楚 Django 日志是如何工作的。它应该表现得像那样还是我的代码有问题?另外,如何将 "Search term is not valid" 保存到 debug.log 文件中?非常感谢!

尝试给出文件的完整路径'filename': '/path/to/django/debug.log'

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/path/to/django/debug.log',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

参考:https://docs.djangoproject.com/en/2.1/topics/logging/#examples

使用您在 settings.py 中提供的名称,在您的情况下是 django

import logging

# use name that you have given in settings in your case it is  django
logger = logging.getLogger('django')

def my_view(request, arg1, arg):
    ...
    if bad_mojo:
        # Log an error message
        logger.error('Something went wrong!')