django logging只记录ERROR部分,DEBUG和INFO不记录

在使用django的logging的时候,配置之后,测试发现,只有调用ERROR的时候,信息才会被写到文件,DEBUG和INFO部分没有被记录,请问大家有没有遇到过类似的情况,多谢。

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse',
        },
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'formatters': {
        'simple': {
            'format': '[%(asctime)s] %(levelname)s %(message)s',
        'datefmt': '%Y-%m-%d %H:%M:%S'
        },
        'verbose': {
            'format': '[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s',
        'datefmt': '%Y-%m-%d %H:%M:%S'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'development_logfile': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': '/data/log/django/dyd/django_dev.log',
            'maxBytes': 1024 * 1024 * 5, # 5 MB
            'backupCount': 7,
            'formatter': 'verbose'
        },
        'production_logfile': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': '/data/log/django/dyd/django_production.log',
            'maxBytes': 1024 * 1024 * 5, # 5 MB
            'backupCount': 7,
            'formatter': 'simple'
        },
        'dba_logfile': {
            'level': 'DEBUG',
            'filters': ['require_debug_false','require_debug_true'],
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': '/data/log/django/dyd/django_dba.log',
            'maxBytes': 1024 * 1024 * 5, # 5 MB
            'backupCount': 7,
            'formatter': 'simple'
        },
    },
    'loggers': {
        'dyd': {
            'handlers': ['development_logfile','production_logfile'],
         },
        'invitesport': {
            'handlers': ['development_logfile','production_logfile'],
         },
        'dba': {
            'handlers': ['console','dba_logfile'],
        },
        'django': {
            'handlers': ['console','development_logfile','production_logfile'],
        },
        'py.warnings': {
            'handlers': ['console','development_logfile'],
        },
    }
}

测试代码:

>>> import logging
>>> logg=logging.getLogger('dyd')
>>> logg.info('adsfasdf')
>>> logg.error('adsfasdf')

文件内容:

[2015-08-26 18:13:43] ERROR [dyd.<module>:1] adsfasdf
回复
阅读 6.4k
2 个回答

重新测试了一下,warning也是可以记录的。

logg.warning

请问大家有没有遇到过类似问题
还是说我哪里配置的不对,
多谢

楼主使用的loger是'dyd'
查看dyd所对应的handler,包括:['development_logfile','production_logfile'],
再查看development_logfile的级别:'level': 'DEBUG',
production_logfile的级别:'level': 'ERROR',
所以当然只会记录到ERROR和DEBUG的级别了
想要记录到INFO,修改handler对应的level即可

宣传栏