Python 即使处理程序级别设置为调试,记录器也不显示调试级别消息
Python logger not showing DEBUG level messages even though handler level is set to debug
我正在尝试创建一个打印“调试”消息的记录器。我已经定义了具有正确级别集的处理程序。它仍然只打印警告级别及更高级别。
这是我的代码:
import logging
# Create a custom logger
logger = logging.getLogger(__name__)
# Create handlers
c_handler = logging.StreamHandler()
f_handler = logging.FileHandler('file.log')
c_handler.setLevel(logging.DEBUG)
f_handler.setLevel(logging.DEBUG)
# Create formatters and add it to handlers
c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
c_handler.setFormatter(c_format)
f_handler.setFormatter(f_format)
# Add handlers to the logger
logger.addHandler(c_handler)
logger.addHandler(f_handler)
logger.warning('This is a warning')
logger.error('This is an error')
logger.info('This is an info')
logger.debug('This is a debug')
虽然这是我得到的控制台输出:
__main__ - WARNING - This is a warning
__main__ - ERROR - This is an error
Process finished with exit code 0
我发现 关于这个问题,但在那种情况下,处理程序没有设置。我的处理程序已设置。
这是怎么回事?
PS:代码是 this page 部分“使用处理程序”部分的略微改动版本。
您忘记设置 logger
等级
import logging
# Create a custom logger
logger = logging.getLogger(__name__)
# Create handlers
c_handler = logging.StreamHandler()
f_handler = logging.FileHandler('file.log')
# handler level is different from logger level
c_handler.setLevel(logging.DEBUG)
f_handler.setLevel(logging.DEBUG)
# this will do the trick
logger.setLevel(logging.DEBUG)
# Create formatters and add it to handlers
c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
c_handler.setFormatter(c_format)
f_handler.setFormatter(f_format)
# Add handlers to the logger
logger.addHandler(c_handler)
logger.addHandler(f_handler)
logger.warning('This is a warning')
logger.error('This is an error')
logger.info('This is an info')
logger.debug('This is a debug')
您可以找到更多信息here
我正在尝试创建一个打印“调试”消息的记录器。我已经定义了具有正确级别集的处理程序。它仍然只打印警告级别及更高级别。
这是我的代码:
import logging
# Create a custom logger
logger = logging.getLogger(__name__)
# Create handlers
c_handler = logging.StreamHandler()
f_handler = logging.FileHandler('file.log')
c_handler.setLevel(logging.DEBUG)
f_handler.setLevel(logging.DEBUG)
# Create formatters and add it to handlers
c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
c_handler.setFormatter(c_format)
f_handler.setFormatter(f_format)
# Add handlers to the logger
logger.addHandler(c_handler)
logger.addHandler(f_handler)
logger.warning('This is a warning')
logger.error('This is an error')
logger.info('This is an info')
logger.debug('This is a debug')
虽然这是我得到的控制台输出:
__main__ - WARNING - This is a warning
__main__ - ERROR - This is an error
Process finished with exit code 0
我发现
这是怎么回事?
PS:代码是 this page 部分“使用处理程序”部分的略微改动版本。
您忘记设置 logger
等级
import logging
# Create a custom logger
logger = logging.getLogger(__name__)
# Create handlers
c_handler = logging.StreamHandler()
f_handler = logging.FileHandler('file.log')
# handler level is different from logger level
c_handler.setLevel(logging.DEBUG)
f_handler.setLevel(logging.DEBUG)
# this will do the trick
logger.setLevel(logging.DEBUG)
# Create formatters and add it to handlers
c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
c_handler.setFormatter(c_format)
f_handler.setFormatter(f_format)
# Add handlers to the logger
logger.addHandler(c_handler)
logger.addHandler(f_handler)
logger.warning('This is a warning')
logger.error('This is an error')
logger.info('This is an info')
logger.debug('This is a debug')
您可以找到更多信息here