【问题标题】:Django logger.error does not save logging entry into the debug.log fileDjango logger.error 不会将日志记录条目保存到 debug.log 文件中
【发布时间】:2019-03-06 00:35:14
【问题描述】:

我遵循 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')

“搜索词无效”这句话确实会在控制台中打印出来,但不会保存到 debug.log 文件中。

我不太确定 Django 日志是如何工作的。它应该表现得像那样还是我的代码有问题?另外,如何将“搜索词无效”保存到 debug.log 文件中?非常感谢!

【问题讨论】:

    标签: django django-logging


    【解决方案1】:

    使用您在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!')
    

    【讨论】:

      【解决方案2】:

      尝试给出文件的完整路径'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

      【讨论】:

        猜你喜欢
        • 2015-09-19
        • 2018-12-24
        • 2016-08-02
        • 2017-07-10
        • 1970-01-01
        • 2011-10-06
        • 1970-01-01
        • 2011-10-03
        • 2015-12-29
        相关资源
        最近更新 更多