Python Logging - Disable logging from imported modules

前端 未结 10 834
清酒与你
清酒与你 2020-12-12 12:52

I\'m using the Python logging module, and would like to disable log messages printed by the third party modules that I import. For example, I\'m using something like the fo

10条回答
  •  没有蜡笔的小新
    2020-12-12 13:36

    This disables all existing loggers, such as those created by imported modules, while still using the root logger (and without having to load an external file).

    import logging.config
    logging.config.dictConfig({
        'version': 1,
        'disable_existing_loggers': True,
    })
    

    Note that you need to import all modules you don't want logged first! Otherwise those won't be considered as "existing loggers". It will then disable all loggers from those modules. This might lead you to also miss out on important errors!

    For more detailed examples using related options for configuration, see https://gist.github.com/st4lk/6287746, and here is a (partially working) example using YAML for config with the coloredlog library.

提交回复
热议问题