【问题标题】:Django info and debug logs ignored, despite configuration尽管配置,Django 信息和调试日志被忽略
【发布时间】:2019-05-21 19:11:11
【问题描述】:

所以我正在尝试在 Django 程序中设置日志记录。我在 settings.py 中设置了日志记录配置:

    DEBUG = True
    LOGGING_CONFIG = None
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'custom': {
                'format': '%(asctime)s %(levelname)-8s %(name)-15s %(message)s'
            }
        },
        'handlers': {
            'console': {
                'level': 'NOTSET',
                'class': 'logging.StreamHandler',
                'formatter': 'custom'
            }
        },
        'loggers': {
            '': {
                'handlers': ['console'],
            }
        }
    }
    import logging.config

    logging.config.dictConfig(LOGGING)

然后我执行以下操作:

import logging
logger = logging.getLogger(__name__)

logger.info("INFO")
logger.debug("DEBUG")
logger.warn("WARN")
logger.critical("CRITICAL")
logger.error("ERROR")

但我只得到以下输出:

2019-05-21 14:08:31,877 WARNING  dashboards.charts WARN
2019-05-21 14:08:31,877 CRITICAL dashboards.charts CRITICAL
2019-05-21 14:08:31,877 ERROR    dashboards.charts ERROR

我尝试将级别更改为 DEBUG 或 info,但这并没有改变任何内容。格式化程序正常工作,所以我不知道为什么关卡不起作用。

【问题讨论】:

    标签: python django logging


    【解决方案1】:

    尝试将level 放在loggers 中,而不是handlers。 例如:

    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'custom'
        }
    },
    'loggers': {
        '': {
            'handlers': ['console'],
            'level': 'DEBUG'
        }
    }
    

    【讨论】:

    • 是的,有效!奇怪的是,在处理程序中设置级别是行不通的。
    猜你喜欢
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多