Django:如何将日志级别设置为INFO或DEBUG [英] Django: how to set log level to INFO or DEBUG

查看:1023
本文介绍了Django:如何将日志级别设置为INFO或DEBUG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将Django中的调试级别更改为DEBUG,因为我想为我的代码添加一些调试消息。它似乎没有效果。

I tried to change the debug level to DEBUG in Django because I want to add some debug messages to my code. It seems to have no effect.

我的日志配置:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'console':{
            'level':'DEBUG',
            'class':'logging.StreamHandler',
            'formatter': 'simple'
        },
    },
    'loggers': {
        'django.request':{
            'handlers': ['console'],
            'propagate': False,
            'level': 'DEBUG',
        },
    },
}

代码:

import logging ; logger = logging.getLogger(__name__)
logger.debug("THIS MESSAGE IS NOT SHOWN IN THE LOGS")
logger.warn("THIS ONE IS")

控制台输出:

WARNING:core.handlers:THIS ONE IS

我也尝试设置DEBUG = False,DEBUG = True我的设置文件。任何想法?

I also have tried setting DEBUG = False and DEBUG = True in my settings file. Any ideas?

编辑:如果我直接在记录器上设置日志级别,它的工作原理是:

If I set the log level on the logger directly, it works:

import logging ; logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.debug("THIS MESSAGE IS NOT SHOWN IN THE LOGS")

输出:

DEBUG:core.handlers:THIS MESSAGE IS NOT SHOWN IN THE LOGS
WARNING:core.handlers:THIS ONE IS

但是,似乎配置文件完全被忽略。即使将配置中的两个条目设置为ERROR,它也会打印这两个语句始终。这是正确的行为还是我还缺少什么?

But: It seems the config file is totally ignored. It prints both statements always even if I set both entries in the config back to ERROR. Is this the correct behaviour or am I still missing something?

推荐答案

您需要添加例如

'core.handlers': {
    'level': 'DEBUG',
    'handlers': ['console']
}

django.request 条目或

'root': {
    'level': 'DEBUG',
    'handlers': ['console']
}

与记录器条目。这将确保在您实际使用的记录器上设置级别,而不仅仅是 django.request 记录器。

in parallel with the 'loggers' entry. This will ensure that the level is set on the logger you are actually using, rather than just the django.request logger.

更新:要显示所有模块的消息,只需在 django.request 之间添加条目即可包含顶级模块,例如 api 处理程序 core 或其他。由于您没有准确说出您的包/模块层次结构,我无法更具体说明。

Update: To show messages for all your modules, just add entries alongside django.request to include your top level modules, e.g. api, handlers, core or whatever. Since you haven't said exactly what your package/module hierarchy is, I can't be more specific.

这篇关于Django:如何将日志级别设置为INFO或DEBUG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆