Python: logging.streamhandler is not sending logs to stdout

You have to set the level of the logger, not only the level of the handler:

mylogger.setLevel(logging.DEBUG)

Here is a nice graphic of the logging workflow, where you can see that either the logger and the handler check for the log level:

http://docs.python.org/2/howto/logging.html#logging-flow

The default logLevel is WARNING, so even if you set the level of your handler to DEBUG, the message will not get through, since your logger suppresses it (it’s also by default WARNING).

By the way, you can do some basic formatting with Formatter:

import logging
import sys

mylogger = logging.getLogger("mylogger")

formatter = logging.Formatter('[%(levelname)s] %(message)s')

handler = logging.StreamHandler(stream=sys.stdout)
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)

mylogger.addHandler(handler)
mylogger.setLevel(logging.DEBUG)

mylogger.debug("This is a debug message.")
mylogger.info("Some info message.")
mylogger.warning("A warning.")

Will give you the output

[DEBUG] This is a debug message.
[INFO] Some info message.
[WARNING] A warning.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)