【问题标题】:Custom Django Log Config with Default Config使用默认配置自定义 Django 日志配置
【发布时间】:2015-02-26 23:51:20
【问题描述】:

我正在尝试设置我的 Django 应用,但在 settings.py 文件中配置登录时遇到问题。

Django 文档和 Python 的日志记录文档指出使用: 'disable_existing_loggers': False 将允许我使用现有的日志记录配置,这样我就不必重复自己了。在这种情况下,默认日志记录是在django.utils.log.py 中找到的 DEFAULT_LOGGING dict

当我尝试使用 DEFAULT_LOGGINGrequire_debug_true 中的现有过滤器时,我在 settings.py 中的 LOGGING_CONFIG 处理程序之一中尝试运行 runserver 时出现 KeyError。

尝试在我的记录器中使用现有处理程序时,我也会遇到同样的错误,例如console。我能想到的唯一原因是 Django 以某种方式无视 disable_existing_loggers 标志。

以前有人遇到过这个问题吗?感谢您的帮助。

   File "/usr/lib/python3.4/logging/config.py", line 750, in add_handlers

       logger.addHandler(self.config['handlers'][h])
     File "/usr/lib/python3.4/logging/config.py", line 317, in __getitem__
       value = dict.__getitem__(self, key)
   KeyError: 'console'

   During handling of the above exception, another exception occurred:

   Traceback (most recent call last):
     File "/usr/lib/python3.4/logging/config.py", line 611, in configure
       self.configure_logger(name, loggers[name])
     File "/usr/lib/python3.4/logging/config.py", line 775, in configure_logger
       self.common_logger_config(logger, config, incremental)

     File "/usr/lib/python3.4/logging/config.py", line 767, in common_logger_config
       self.add_handlers(logger, handlers)
     File "/usr/lib/python3.4/logging/config.py", line 752, in add_handlers
       raise ValueError('Unable to add handler %r: %s' % (h, e))
   ValueError: Unable to add handler 'console': 'console'



LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'activity': {
            'format': '[%(levelname)s] %(pathname)s <%(funcName)s>[%(lineno)s] : %(message)s',
        },
        'debug': {
            'format': '[%(levelname)s] %(pathname)s <%(funcName)s>[%(lineno)s] : %(message)s',
        },
    },
    'handlers': {
        'debug': {
            'level': 'DEBUG',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'filename': '/var/www/app/log/debug.log',
            'formatter': 'debug',
            'backupCount': 48,
            'when': 'H',
        },
        'activity': {
            'level': 'INFO',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'filename': '/var/www/app/log/activity.log',
            'formatter': 'activity',
            'backupCount': 48,
            'when': 'H',
        },
        'error': {
            'level': 'ERROR',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'filename': '/var/www/app/log/error.log',
            'formatter': 'activity',
            'backupCount': 48,
            'when': 'H',
        },
        'syslog': {
            'level': 'INFO',
            'class': 'logging.handlers.SysLogHandler',
            'formatter': 'activity',
            'facility': SysLogHandler.LOG_LOCAL2,
            'address': '/dev/log',
        },
    },
    'loggers': {
        'app.activity': {
            'handlers': ["activity", "error", "debug"],
            'level': 'DEBUG',
            'propagate': True,
        },
        'django.request': {
            'handlers': ["mail_admins", "error", "activity", "debug"],
            'level': 'ERROR',
            'propagate': False,
        },
        'django.security': {
            'handlers': ["mail_admins", "error", "activity"],
            'level': 'ERROR',
            'propagate': False,
        },
        'py.warnings': {
            'handlers': ["console", "debug"],
        },
    },
}

【问题讨论】:

  • 能否在 settings.py 中显示您的日志记录配置
  • 我想这可能会有所帮助...已添加。

标签: python django logging


【解决方案1】:

保持默认日志记录配置并仅自定义某些设置的最简单方法是更新 Django 用于配置日志记录的django.utils.log.DEFAULT_LOGGING

from django.utils.log import DEFAULT_LOGGING

# Enable logging to console from our modules by configuring the root logger
DEFAULT_LOGGING['loggers'][''] = {
    'handlers': ['console'],
    'level': 'INFO',
    'propagate': True
}

这必须在settings.py 中,因为在导入设置后将立即配置日志记录。

【讨论】:

  • 为一些简单的调试目的省去了很多麻烦。
【解决方案2】:

在我看来,您需要将console Handler 的配置以及django.utils.log.py 中的其余处理程序、过滤器和格式化程序(您使用)添加到日志记录配置中。

disable_existing_loggers 参数仅与记录器有关(与处理程序、过滤器和格式化程序无关)

【讨论】:

  • 我认为您可能是对的,必须仔细阅读文档。记录器!=记录配置。感谢收看。
猜你喜欢
  • 2018-09-17
  • 1970-01-01
  • 2017-03-05
  • 2014-07-22
  • 1970-01-01
  • 2011-07-23
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
相关资源
最近更新 更多