python logging: logger setLevel() is not enforced?

江枫思渺然 提交于 2021-02-07 10:18:29

问题


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.error("hi") DOES print to the log

I think it has something to do with level but i specifically set it to logging.DEBUG

Anyone know what I am doing wrong?


回答1:


In your case rock_logger is the logger and hdlr is the handler. 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. So it's not passing the messages to the handler when you try info() or debug().

rock_logger.setLevel(logging.DEBUG)

That should fix the issue.



来源:https://stackoverflow.com/questions/38286641/python-logging-logger-setlevel-is-not-enforced

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!