1

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:%S

Then, 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'
askedNov 2, 2016 at 18:58
RNA's user avatar
2
  • 1
    I believe (though not 100% certain) it's becausesetLevel expects 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 importlogging at the top of the file)CommentedNov 2, 2016 at 19:01
  • in python 3.5, you can set with either numeric or string as well.CommentedNov 2, 2016 at 19:17

2 Answers2

4

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.

answeredNov 2, 2016 at 19:37
Savir's user avatar
Sign up to request clarification or add additional context in comments.

2 Comments

Seriously, I told myself to try the "good practice" of using thelogging 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...
This answer provides another solution to your problem (calling basicConfig):stackoverflow.com/a/46700675
0

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,)
answeredNov 2, 2016 at 19:01

2 Comments

Thanks. note I set the level after thefileConfig call. Can't you change the level after reading the config file?
I see. Would you be able to printl.getEffectiveLevel() to tell us what level is set before and after you dol.setLevel('DEBUG') ?

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.