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

Python Django:如何将日志级别设置为INFO或DEBUG,python,django,logging,configuration-files,Python,Django,Logging,Configuration Files,我试图在Django中将调试级别更改为debug,因为我想在代码中添加一些调试消息。这似乎没有效果 我的日志记录配置: LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'simple': { 'format': '%(levelname)s %(message)s' }, }, 'handlers

我试图在Django中将调试级别更改为debug,因为我想在代码中添加一些调试消息。这似乎没有效果

我的日志记录配置:

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',
        },
    },
}
代码:

控制台上的输出:

WARNING:core.handlers:THIS ONE IS
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'],
        },
    },
})
我还尝试在设置文件中设置DEBUG=False和DEBUG=True。有什么想法吗

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

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

您需要添加例如

'core.handlers': {
    'level': 'DEBUG',
    'handlers': ['console']
}
django.request
条目并行,或

'root': {
    'level': 'DEBUG',
    'handlers': ['console']
}
与“记录者”条目并行。这将确保在您实际使用的记录器上设置级别,而不仅仅是
django.request
logger

更新:要显示所有模块的消息,只需在
django旁边添加条目。请求
包含顶级模块,例如
api
处理程序
core
等。由于您还没有确切说明您的包/模块层次结构是什么,因此我无法更具体地说明。

我通过更改

LOGGING = {
    ...
}
致:

例如,要将所有消息记录到控制台,请执行以下操作:

WARNING:core.handlers:THIS ONE IS
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'],
        },
    },
})

快速检查:
logger.warn(“日志级别设置为{0}”)。格式(logging.getLevelName(logger.level))
WARNING:core.handlers:Log-level设置为NOTSET。所以这是默认的警告级别,对吗?为什么Django不根据我的设置进行相应的更改?眨眼啊。我什么都没有。在我看来,你做的一切都是对的。我更新了我的答案以回应你的评论。这很有效。但是有没有办法只显示我的调试日志?如果我设置了“root”条目,我会得到日志中所有我不感兴趣的sql查询。设置core.handlers会将调试输出限制为core.handlers。但我希望在我的文件中看到所有调试输出(所有handlers.py、api.py和我的其他文件)。@kev找到解决方案了吗?:)某种程度上。我已经创建了一个自定义文件记录器,如下所示:,import logging logger=logging.getLogger('custom')logger.setLevel(logging.DEBUG)问题是关于Django的,您只是在这里绕过整个Django日志系统。。。