【问题标题】:In Django: Can I set logging level highest than CRITICAL?在 Django 中:我可以将日志记录级别设置为高于 CRITICAL 吗?
【发布时间】:2013-12-06 18:29:20
【问题描述】:

我在 Django 1.5 中有一个项目,并且有一个用于调试、信息和错误的日志记录处理程序。

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'console':{
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'file_debug':{
            'level':'DEBUG',
            'class':'logging.handlers.TimedRotatingFileHandler',
            'filename': os.path.join(SITE_ROOT, '..','logs','DEBUG.log'),
            'when':'midnight',
            'interval':1,
            'backupCount': 4,
            'formatter':'simple',
            'filters':['require_debug_true']
        },
        'file_info':{
            'level':'INFO',
            'class':'logging.handlers.TimedRotatingFileHandler',
            'filename': os.path.join(SITE_ROOT, '..','logs','INFO.log'),
            'when':'midnight',
            'interval':1,
            'backupCount': 4,
            'formatter':'simple',
        },
        'file_error':{
            'level':'ERROR',
            'class':'logging.handlers.TimedRotatingFileHandler',
            'filename': os.path.join(SITE_ROOT, '..','logs','ERROR.log'),
            'when':'midnight',
            'interval':1,
            'backupCount': 4,
            'formatter':'simple',
        },
    },
    'loggers': {
        'django.request':{
            'handlers': ['console'],
            'propagate': False,
            'level': 'DEBUG',
        },
    },
}

但是我想要一个只包含特定内容的日志文件,我不希望在其中打印任何错误。我认为使用 CRITICAL 级别可能会解决我的问题,但由于我的日志中的信息仅用于报告信息,因此具有高于 CRITICAL 的通用级别可能会变得很少,例如:

        'file_report':{
            'level':'REPORT',
            'class':'logging.handlers.TimedRotatingFileHandler',
            'filename': os.path.join(SITE_ROOT, '..','logs','REPORT.log'),
            'when':'midnight',
            'interval':1,
            'backupCount': 4,
            'formatter':'simple',
        },

我的案例的最佳做法是什么?

【问题讨论】:

    标签: python django logging


    【解决方案1】:

    您应该配置一个自定义记录器并向其发送消息。例如,假设你的包名为foo

    LOGGING = {
        ...
        'handlers': {
            ...
            'file_report': {
                'level': 'INFO',
                'class': 'logging.handlers.TimedRotatingFileHandler',
                'filename': os.path.join(SITE_ROOT, '..', 'logs', 'REPORT.log'),
                'when':'midnight',
                'interval': 1,
                'backupCount': 4,
                'formatter':'simple',
            },
        },
        'loggers': {
            'django.request': {
                'handlers': ['console'],
                'propagate': False,
                'level': 'DEBUG',
            },
            'foo.report': {
                'handlers': ['file_report'],
                'propagate': False,
                'level': 'INFO',
            },
        },
    }
    

    然后在您的应用程序代码中:

    import logging
    logging.getLogger('foo.report').info('something something')
    

    【讨论】:

      猜你喜欢
      • 2016-11-27
      • 2011-07-26
      • 2012-02-17
      • 2020-04-16
      • 1970-01-01
      • 2015-12-02
      • 2023-03-23
      • 2011-05-06
      • 1970-01-01
      相关资源
      最近更新 更多