Computer >> Computer tutorials >  >> Programming >> Python

How to disable logging from imported modules in Python?


You can disable logging from imported modules using the logging module. You can configure it to not log messages unless they are at least warnings using the following code:

import logging
logging.getLogger("imported_module").setLevel(logging.WARNING)

If you dont want to write the module name as a string, you can also use imported_module.__name__. If you want to go a level higher and only want to log messages when they are errors or critical, you can do replace logging.WARNING with logging.ERROR and logging.CRITICAL respectively.