推荐学习书目
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
dsg001
V2EX  ›  Python

logging 为嘛输出两次?

  •  
  •   dsg001 · Sep 19, 2016 · 3974 views
    This topic created in 3547 days ago, the information mentioned may be changed or developed.

    import logging as log logger = log.getLogger('logger') logger.addHandler(log.StreamHandler()) logger.setLevel(log.INFO) log.info('') logger.info('test logger')

    test logger INFO:logger:test logger

    平时使用 logger 完全没问题,现在碰到一个直接 root 输出的第三方库才发现有这问题

    2 replies    2016-09-19 19:56:58 +08:00
    sylecn
        1
    sylecn  
       Sep 19, 2016   ❤️ 2
    代码格式乱掉了,基本没法看。重新排版一下吧。

    一般输出两次是配置问题,在子 logger 配置中,如果不希望上游的 logger 继续收到并处理该日志,需要设置 propagate=0.

    根 logger 是空字符串"", 子 logger 就是 "a.b.c"这种。一般第三方库都会用子 logger 。但是你没有点名,所以不能确定。如果真的是用的 root logger ,那除非是配置了两个 console 类型的 handler ,才会输出两次。
    dsg001
        2
    dsg001  
    OP
       Sep 19, 2016
    @sylecn 感谢,设置 propagate 搞定

    第三方直接使用的 root logger ,貌似 root 使用后会自动添加一个 StreamHandler , root 和子 logger 各有一个 StreamHandler ,所以输出两次记录
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2999 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 07:16 · PVG 15:16 · LAX 00:16 · JFK 03:16
    ♥ Do have faith in what you're doing.