Larold
Larold

Reputation: 670

Why doesn't Python logger display info or debug message at level logging.INFO?

Can someone please explain to me why the .info() and .debug() calls do not print anything, even when it seems like they should? I feel like there is something very basic I'm not understanding, even after going through the logging module documentation...

$  python                                    
Python 3.6.5 (default, Apr 25 2018, 14:23:58) 
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logger = logging.getLogger()
>>> logger.setLevel(logging.INFO)
>>> logger.warning('This is a warning. We should see it.')
This is a warning. We should see it.
>>> logger.debug('This is a debug message. We should not see it.')
>>> logger.info('This is an info message. We should... see it, right?')
>>> logger.setLevel(logging.DEBUG)
>>> logger.info('This is an info message. We should... see it, right?')
>>> logger.debug('Weird. So I guess we are not going to see this as well?')
>>> 

Upvotes: 1

Views: 348

Answers (1)

scnerd
scnerd

Reputation: 6113

It's logging, just not to terminal. If you want to see the logs output to your terminal (much like print), you need to add a handler:

logger.addHandler(logging.StreamHandler())

After doing so, logging should display to your terminal as expected.

Upvotes: 4

Related Questions