I am trying to add a logging handler in Python 3 this way:
$ python3
Python 3.5.2 (default, Sep 14 2017, 22:51:06)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>>
>>> logger = logging.getLogger()
>>> handler = logging.StreamHandler()
>>> handler.setLevel(logging.INFO)
>>> logger.addHandler(handler)
>>>
>>> logging.info("my info")
>>> logging.warning("my warning")
my warning
I specified loglevel as INFO
. Why does logging.info("my info")
output nothing?
because you need to set logger
lever as well.
import logging
logger = logging.getLogger()
# here
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
logger.addHandler(handler)
output:
>>> logging.info("my info")
my info
>>> logging.warning("my warning")
my warning
Loggers have levels as well as handlers. The default level is warn so your info is ignored.
To get your example to work, include logger.setLevel(logging.INFO)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With