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

为什么拿 vscode 写 py 在调试时用 logging 输出信息时在控制台会出现 stderr 输出重复内容的问题?

  •  1
     
  •   techmoe · Jul 27, 2016 · 5053 views
    This topic created in 3579 days ago, the information mentioned may be changed or developed.

    这里环境 win10\py3.5\vscode1.3 , vscode 已装那个在推荐列表上高高在上的 py 插件,最近调试 py 时出现了一个很令人强迫症发作的问题
    首先是一个再简单不过的使用 logging 输出 debug 信息的代码段

    import logging
    
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
    
    logging.debug('wtf?')
    

    然后 F5 跑起来,调试器选的是第一个 python
    然后问题出现了

    2016-07-27 22:45:49,739 root         DEBUG    wtf?
    pyProc.stderr: 2016-07-27 22:45:49,739 root         DEBUG    wtf?
    

    其实我目前来写 py 半年还不到,几经查资料得知stderr 是用来输出标准错误信息的,但是我实在想不通上面已经输出一遍错误信息了为什么下面还会再以 stderr 的身份再输出一遍?有趣的事这段程序在 cmd 下直接 python 运行没有这种问题出现。不知道有没有拿 vscode 写 py 的人遇到过类似的问题?或者还是我哪里理解错了还是配置有问题?这样的问题该怎么解决?求指教

    3 replies    2016-07-28 09:57:28 +08:00
    techmoe
        1
    techmoe  
    OP
       Jul 27, 2016
    solved..
    我强制把 logging 的输出流指定为 sys.stdout 就好了。。
    如此一来难道说 logging 的默认输出是输出到 stderr 吗?然后 vscode 的那个 python 插件还自作聪明的加 stderr 前缀又显示了一遍
    weyou
        2
    weyou  
       Jul 28, 2016
    basicConfig 默认调用的 StreamHandler , StreamHandler 默认输出到 stderr 。
    指定 stream 就好了
    21grams
        3
    21grams  
       Jul 28, 2016
    pycharm 多好,不用操心这些事
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1165 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 18:11 · PVG 02:11 · LAX 11:11 · JFK 14:11
    ♥ Do have faith in what you're doing.