简体   繁体   中英

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?')
>>> 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM