问题
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 BUTrock_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