简体   繁体   English

将日志级别设置为logging.DEBUG或logging.INFO无效

[英]Setting log level to logging.DEBUG or logging.INFO has no effect

I am trying to do a pretty simple logging setup. 我正在尝试做一个非常简单的日志设置。 I just want all of my log output to go to the terminal and to my log file. 我只想将我的所有日​​志输出转到终端和我的日志文件。 I found the below example on Real Python that demonstrates the setup of stream and file log handlers: 我在Real Python上找到了以下示例,演示了流和文件日志处理程序的设置:

# logging_example.py

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.WARNING)
f_handler.setLevel(logging.ERROR)

# 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')

## Log Output
# 2019-08-31 22:16:02,478 - __main__ - WARNING - This is a warning
# 2019-08-31 22:16:02,478 - __main__ - ERROR - This is an error

And this logs to the console and to the file as you would expect. 这会按照您的预期记录到控制台和文件中。 However, when I modify the program so that it will also log INFO, like so: 但是,当我修改程序以便它也会记录INFO时,如下所示:

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('logs')
logger.error('logs')
logger.info('should log but doesn\'t')
logger.debug('should log but doesn\'t')

## Log Output
# __main__ - WARNING - logs
# __main__ - ERROR - logs

What am I doing wrong? 我究竟做错了什么? Any help would be greatly appreciated! 任何帮助将不胜感激!

You will also need to invoke setLevel(level) on the logger object itself, as, by default, it will use ROOT 's logging level (if it doesn't have any other ancestors), which is WARNING : 您还需要在logger对象本身上调用setLevel(level) ,因为默认情况下它将使用ROOT的日志记录级别(如果它没有任何其他祖先),这是WARNING

logger.setLevel(logging.DEBUG)

Full code with output: 带输出的完整代码:

import logging

logger = logging.getLogger(__name__)

# Create handlers
c_handler = logging.StreamHandler()
f_handler = logging.FileHandler('file.log')
logger.setLevel(logging.DEBUG) # <<< Added Line
c_handler.setLevel(logging.DEBUG)
f_handler.setLevel(logging.INFO)

# 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('logs')
logger.error('logs')
logger.info('should log but doesn\'t')
logger.debug('should log but doesn\'t')

Output: 输出:

__main__ - WARNING - logs
__main__ - ERROR - logs
__main__ - INFO - should log but doesn't
__main__ - DEBUG - should log but doesn't

there is two place you can set level: Logger , Handler . 你可以设置两个级别: LoggerHandler both of them will affect the output of your logging setting -- in different node of the logging flow. 它们都会影响日志记录设置的输出 - 在日志记录流的不同节点中。 also you can add Fliter to these two instance to filter log record with more sophisticate rule. 您还可以将Fliter添加到这两个实例中,以使用更复杂的规则来过滤日志记录。

flowing char in the office docment make clear what happening when logging. 办公室文件中流动的字符清楚地表明日志记录时发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM