【问题标题】:Django logging not working in viewsDjango 日志记录在视图中不起作用
【发布时间】:2016-03-31 09:31:31
【问题描述】:

我在我的 Django 1.9 项目中设置了日志记录。当我在views.py 中使用日志记录时,我按预期自动获取django_request.log 中的日志,但未在mylog.log 中获取日志。我哪里错了?

views.py

import logging
logr = logging.getLogger(__name__)

def sample_view(request):
   logr.debug('Ran Sample view')
   return HttpResponse("Done!")

settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
        },
    },
    'handlers': {
        'default': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'logs/mylog.log',
            'maxBytes': 1024 * 1024 * 5,  # 5MB
            'backupCount': 5,
            'formatter': 'standard'
        },
        'request_handler': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'logs/django_request.log',
            'maxBytes': 1024 * 1024 * 5,  # 5MB
            'backupCount': 5,
            'formatter': 'standard'
        },
    },
    'loggers': {
        '': {
            'handlers': ['default'],
            'level': 'DEBUG',
            'propagate': True
        },
        'django.request': {
            'handlers': ['request_handler'],
            'level': 'DEBUG',
            'propagate': False
        },
    }
}

【问题讨论】:

    标签: django django-1.9 django-logging


    【解决方案1】:

    我发现了这个“如果 LOGGING dictConfig 中的 disable_existing_loggers 键设置为 True(这是默认设置),那么默认配置中的所有记录器都将被禁用。禁用的记录器与删除的记录器不同; logger 仍然存在,但会默默地丢弃任何记录到它的内容,甚至不会将条目传播到父 logger。"

    https://docs.djangoproject.com/en/1.7/topics/logging/#configuring-logging

    因此,如果您设置'disable_existing_loggers': False,,您将能够看到所有日志。

    【讨论】:

      猜你喜欢
      • 2020-10-28
      • 1970-01-01
      • 2020-05-27
      • 2011-07-12
      • 2021-11-19
      • 2012-06-13
      • 2018-08-24
      • 2011-12-27
      • 2013-04-27
      相关资源
      最近更新 更多