推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
fangwenxue
V2EX  ›  Python

Python log 阻止其他类打印?

  •  
  •   fangwenxue · Feb 21, 2021 · 2662 views
    This topic created in 2043 days ago, the information mentioned may be changed or developed.
    DEFAULT_FORMATTER = '%(asctime)s[%(filename)s:%(lineno)d][%(levelname)s]:%(message)s'
    logging.basicConfig(format=DEFAULT_FORMATTER, level=logging.INFO)
    

    • 调用了第三方类, 结果一堆的打印
    • 如何阻止第三方类的打印?
    4 replies  •  2021-02-23 12:52:24 +08:00
    Rhilip
        1
    Rhilip  
       Feb 21, 2021
    把对应 logger 的等级提高,例如可以通过如下方法阻止 requests 模块的 log 打印

    ```
    logging.getLogger("urllib3").setLevel(logging.WARNING)
    logging.getLogger("requests").setLevel(logging.WARNING)
    ```
    no1xsyzy
        2
    no1xsyzy  
       Feb 21, 2021
    单独配置 handler,参考 logging 库文档和 HOWTOs 的一篇
    simple2025
        3
    simple2025  
       Feb 21, 2021
    应该是你设置了 root logger, 其他日志 log 的时候, propogate 导致的,
    frostming
        4
    frostming  
       Feb 23, 2021
    不要用 basicConfig 去污染其它 logger,而应该用自己的 Logger:

    logger = logging.getLogger("myapp")
    logger.setLevel(logging.INFO)
    ...

    这跟全局依赖和虚拟环境的思想是类似的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   844 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 20:49 · PVG 04:49 · LAX 13:49 · JFK 16:49
    ♥ Do have faith in what you're doing.