Python Django:如何将日志级别设置为 INFO 或 DEBUG

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19720785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:26:08  来源:igfitidea点击:

Django: how to set log level to INFO or DEBUG

pythondjangologgingconfiguration-files

提问by kev

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.

我试图在 Django 中将调试级别更改为 DEBUG,因为我想向我的代码添加一些调试消息。好像没什么效果。

My logging configuration:

我的日志配置:

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',
        },
    },
}

Code:

代码:

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

Output on the console:

控制台输出:

WARNING:core.handlers:THIS ONE IS

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

我也试过在我的设置文件中设置 DEBUG = False 和 DEBUG = True 。有任何想法吗?

Edit: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")

Output:

输出:

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

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

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

采纳答案by Vinay Sajip

You need to add e.g.

你需要添加例如

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

in parallel with the django.requestentry, or

django.request条目并行,或

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

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.requestlogger.

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

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

更新:要显示所有模块的消息,只是添加条目旁边django.request,包括您的顶层模块,例如apihandlerscore或什么的。由于您还没有确切说明您的包/模块层次结构是什么,我不能更具体。

回答by Julián Luini

I fixed it by changing

我通过改变来修复它

LOGGING = {
    ...
}

to:

到:

logging.config.dictConfig({
    ...
})

For example to log all messages to the console:

例如,将所有消息记录到控制台:

import logging.config
LOGGING_CONFIG = None
logging.config.dictConfig({
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'console': {
            # exact format is not important, this is the minimum information
            'format': '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'console',
        },
    },
    'loggers': {
    # root logger
        '': {
            'level': 'DEBUG',
            'handlers': ['console'],
        },
    },
})