V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2021-02-21 15:48:41 +08:00 · 1704 次点击
    这是一个创建于 1153 天前的主题,其中的信息可能已经有所发展或是发生改变。
    DEFAULT_FORMATTER = '%(asctime)s[%(filename)s:%(lineno)d][%(levelname)s]:%(message)s'
    logging.basicConfig(format=DEFAULT_FORMATTER, level=logging.INFO)
    

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

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

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

    这跟全局依赖和虚拟环境的思想是类似的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   911 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:00 · PVG 05:00 · LAX 14:00 · JFK 17:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.