[英]Confused about python logging with config file (only logs what is set for root_logger in config file)
I (now) have the following code:我(现在)有以下代码:
import logging
from logging.config import fileConfig
def function1():
logger = logging.getLogger('function1')
logger.debug('Debug')
logger.info('Info')
logger.warning('Warn')
logger.critical('Critical')
def main():
fileConfig('test_log.conf')
logger = logging.getLogger('main')
logger.debug('Debug')
logger.info('Info')
logger.warning('Warn')
logger.critical('Critical')
function1()
if __name__ == '__main__':
main()
And I have the following config file:我有以下配置文件:
[loggers]
keys:root,log1
[handlers]
keys:consoleHandler,rotatingFileHandler
[formatters]
keys:logFormatter
[logger_root]
level:INFO
handlers:rotatingFileHandler,consoleHandler
[logger_log1]
level:DEBUG
handlers:rotatingFileHandler, consoleHandler
qualname:main
propagate=1
[handler_consoleHandler]
class:StreamHandler
formatter:logFormatter
args:(sys.stdout,)
[handler_rotatingFileHandler]
class=logging.handlers.RotatingFileHandler
level=NOTSET
args=('logs/Log.log', 'w', 5000)
formatter=logFormatter
[formatter_logFormatter]
format:%(asctime)s - %(name)s -%(levelname)s - %(message)s
When I run, I get the following output:当我运行时,我得到以下输出:
2020-03-23 19:16:30,529 - main -DEBUG - Debug
2020-03-23 19:16:30,529 - main -DEBUG - Debug
2020-03-23 19:16:30,530 - main -INFO - Info
2020-03-23 19:16:30,530 - main -INFO - Info
2020-03-23 19:16:30,530 - main -WARNING - Warn
2020-03-23 19:16:30,530 - main -WARNING - Warn
2020-03-23 19:16:30,530 - main -CRITICAL - Critical
2020-03-23 19:16:30,530 - main -CRITICAL - Critical
2020-03-23 19:16:30,531 - function1 -INFO - Info
2020-03-23 19:16:30,531 - function1 -WARNING - Warn
2020-03-23 19:16:30,531 - function1 -CRITICAL - Critical
I have given each of the loggers names and I still have the same problem我已经给了每个记录器的名字,但我仍然有同样的问题
I have the following code:我有以下代码:
def main():
import logging
from logging.config import fileConfig
fileConfig('logging.conf')
logger = logging.getLogger()
logger.debug('Debug')
logger.info('Info')
logger.warning('Warn')
logger.critical('Critical')
if __name__ == '__main__':
main()
And I have the following config file:我有以下配置文件:
[loggers]
keys:root,log1
[handlers]
keys:consoleHandler,rotatingFileHandler
[formatters]
keys:logFormatter
[logger_root]
level:INFO
handlers:rotatingFileHandler,consoleHandler
[logger_log1]
level:DEBUG
handlers:rotatingFileHandler, consoleHandler
qualname:main
propagate=1
[handler_consoleHandler]
class:StreamHandler
formatter:logFormatter
args:(sys.stdout,)
[handler_rotatingFileHandler]
class=logging.handlers.RotatingFileHandler
level=NOTSET
args=('logs/Log.log', 'w', 5000)
formatter=logFormatter
[formatter_logFormatter]
format:%(asctime)s - %(name)s -%(levelname)s - %(message)s
When I run, I get the following output:当我运行时,我得到以下输出:
2020-03-23 18:13:54,950 - tedCheck.main -WARNING - Warn
2020-03-23 18:13:54,950 - tedCheck.main -CRITICAL - Critical
What I was expecting was that no matter what the root_logger was set to, I could over-ride it in the specific log section for the function/method of the config file.我期望的是,无论 root_logger 设置为什么,我都可以在配置文件的功能/方法的特定日志部分中覆盖它。
What good is having the section if it is going to do whatever the root_logger is set to?如果它要执行 root_logger 设置的任何内容,那么拥有该部分有什么好处? My guess is that my config file is hosed somehow.
我的猜测是我的配置文件以某种方式被处理了。
This line gets the root logger in the hierarchy, specifically:这一行获取层次结构中的根记录器,特别是:
logger = logging.getLogger()
If you don't want the root logger, then you need to pass a name如果你不想要根记录器,那么你需要传递一个名称
logger = logging.getLogger(name="main")
Note that with your existing configuration, a propagating log1 and console/file handlers configured for root as well, you will likely see duplicate log events.请注意,使用您现有的配置,传播 log1 和为 root 配置的控制台/文件处理程序,您可能会看到重复的日志事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.