How to disable logging on the standard error stream? How to disable logging on the standard error stream? python python

How to disable logging on the standard error stream?


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.


I use:

logger = logging.getLogger()logger.disabled = True... whatever you want ...logger.disabled = False


You can use:

logging.basicConfig(level=your_level)

where your_level is one of those:

'debug': logging.DEBUG,'info': logging.INFO,'warning': logging.WARNING,'error': logging.ERROR,'critical': logging.CRITICAL

So, if you set your_level to logging.CRITICAL, you will get only critical messages sent by:

logging.critical('This is a critical error message')

Setting your_level to logging.DEBUG will show all levels of logging.

For more details, please take a look at logging examples.

In the same manner to change level for each Handler use Handler.setLevel() function.

import loggingimport logging.handlersLOG_FILENAME = '/tmp/logging_rotatingfile_example.out'# Set up a specific logger with our desired output levelmy_logger = logging.getLogger('MyLogger')my_logger.setLevel(logging.DEBUG)# Add the log message handler to the loggerhandler = logging.handlers.RotatingFileHandler(          LOG_FILENAME, maxBytes=20, backupCount=5)handler.setLevel(logging.CRITICAL)my_logger.addHandler(handler)