I have log a file:
[loggers]keys=root[handlers]keys=consoleHandler[formatters]keys=simpleFormatter[logger_root]level=INFOhandlers=consoleHandler[handler_consoleHandler]class=StreamHandlerlevel=INFOformatter=simpleFormatterargs=(sys.stdout,)[formatter_simpleFormatter]format=%(asctime)s %(levelname)s %(message)sdatefmt=%Y-%m-%d %H:%M:%SThen, when I run the following code, it does not print the dubug message. why is that?
from logging import getLoggerfrom logging.config import fileConfigfileConfig('/Users/zech/Dropbox/git/micronota/micronota/log.cfg')l = getLogger()l.setLevel('DEBUG')l.debug('adfa')l.info('info') # this works and outputs 'info'- 1I believe (though not 100% certain) it's because
setLevelexpects a numeric level (docs.python.org/2/library/logging.html#logging-levels) Does it work if you do:l.setLevel(logging.DEBUG)? (you'll need to importloggingat the top of the file)Savir– Savir2016-11-02 19:01:46 +00:00CommentedNov 2, 2016 at 19:01 - in python 3.5, you can set with either numeric or string as well.RNA– RNA2016-11-02 19:17:14 +00:00CommentedNov 2, 2016 at 19:17
2 Answers2
Ah, I got it!
You also need to set the level in your handlers.
In your config file, yourStreamHandler has levelINFO. That means that even though your root loggerl is going to emit thel.debug('adfa') message, the handler itself is not going to register it. This is intended so you can have a finer control on what messages to log. For instance, let's say you want to have two handlers: one that writes to the terminal and another that writes to a file. In the terminal, it might be OK to output everything, but you might get concerned the files get too big, right? So you create aFileHandler with its level set toWARN level, so you could placel.debug(...) statements in your code which would get printed to the console, but not to the file.
If you don't wanna change your configuration file, you can do this:
l = getLogger()l.setLevel('DEBUG')for handler in l.handlers: handler.setLevel('DEBUG')l.debug('adfa')From the docs forPython 3.1 (bit older, but it looks like the sentence I'm gonna paste below was removed in the Python 3.5 docs)
Why are there two setLevel() methods? The level set in the logger determines which severity of messages it will pass to its handlers. The level set in each handler determines which messages that handler will send on.
2 Comments
logging module, but I lost so much time with weird implementation things like that. No doubt it made some sense to do that, but logging is definitely not as simple as it should be in order to be more widely used...You've set your log level asINFO. You can replace:
[logger_root]level=INFOhandlers=consoleHandler[handler_consoleHandler]class=StreamHandlerlevel=INFOformatter=simpleFormatterargs=(sys.stdout,)With
[logger_root]level=DEBUGhandlers=consoleHandler[handler_consoleHandler]class=StreamHandlerlevel=DEBUGformatter=simpleFormatterargs=(sys.stdout,)2 Comments
fileConfig call. Can't you change the level after reading the config file?Explore related questions
See similar questions with these tags.