简体   繁体   English

python日志记录:记录器setLevel()是否未强制执行?

[英]python logging: logger setLevel() is not enforced?

fmtter = logging.Formatter('%(asctime)s,%(msecs)05.1f (%(funcName)s) %(message)s', '%H:%M:%S')

rock_log = '%s/rock.log' % Build.path
hdlr = logging.FileHandler(rock_log, mode='w')
hdlr.setFormatter(fmtter)
hdlr.setLevel(logging.DEBUG)

rock_logger = logging.getLogger('rock')
rock_logger.addHandler(hdlr)

I have the above logger 我有上面的记录器

rock_logger.info("hi") doesnt print anything to the log BUT rock_logger.info("hi")不会将任何内容打印到日志中
rock_logger.error("hi") DOES print to the log rock_logger.error("hi")打印到日志

I think it has something to do with level but i specifically set it to logging.DEBUG 我认为这与级别有关,但我专门将其设置为logging.DEBUG

Anyone know what I am doing wrong? 有人知道我在做什么错吗?

In your case rock_logger is the logger and hdlr is the handler. 在您的情况下, rock_logger是记录器,而hdlr是处理程序。 When you try to log something, the logger first checks if it should handle the message (depending on it's own log level). 当您尝试记录某些内容时,记录器首先检查它是否应该处理该消息(取决于它自己的日志级别)。 Then it passes the message to the handler. 然后它将消息传递给处理程序。 The handler then checks the level against it's own log level and decided whether to write it to the file or not. 然后,处理程序对照其自身的日志级别检查该级别,并决定是否将其写入文件。

Your rock_logger might have the logging level set to errors. 您的rock_logger可能会将日志记录级别设置为error。 So it's not passing the messages to the handler when you try info() or debug() . 因此,当您尝试info()debug()时,不会将消息传递给处理程序。

rock_logger.setLevel(logging.DEBUG)

That should fix the issue. 那应该解决问题。

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

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