How to disable logging on the standard error stream in Python?

sorin picture sorin · Feb 15, 2010 · Viewed 172.9k times · Source

How to disable logging on the standard error stream in Python? This does not work:

import logging

logger = logging.getLogger()
logger.removeHandler(sys.stderr)
logger.warning('foobar')  # emits 'foobar' on sys.stderr

Answer

sorin picture sorin · Feb 15, 2010

I found a solution for this:

logger = logging.getLogger('my-logger')
logger.propagate = False
# now if you use logger it will not log to console.

This will prevent logging from being send to the upper logger that includes the console logging.