Python日志记录和Pydev调试器?

8

编辑:

使用 Liclipse 1.2.1 而非 1.3.0 或 1.4.0 版本可以正常工作。更新日志指出 1.3.0 版本包含 Pydev 3.9.1 和 Eclipse 4.4.1 更新,似乎会破坏日志调试。


使用 Liclipse 和 Pydev 调试器(以及 CPython)运行以下代码示例时,会收到该错误信息:

 logging.config.dictConfig(config)
 File "C:\Python27\lib\logging\config.py", line 794, in dictConfig
   dictConfigClass(config).configure()
 File "C:\Python27\lib\logging\config.py", line 576, in configure
   '%r: %s' % (name, e))
 ValueError: Unable to configure handler 'console': 'DictConfigurator' object has no attribute 'startswith'

没有调试就没有问题,日志模块是否需要运行环境并且仅在其上工作?

以下是使用的代码示例:

import logging.config
import yaml

def setup_logging():    
    default_path = 'logger.conf' 
    default_level = logging.DEBUG

    if os.path.exists(default_path):
        with open(default_path, 'rt') as f:
            config = yaml.load(f.read())
        logging.config.dictConfig(config)
    else:
        logging.basicConfig(level=default_level)

以下是我的logger.conf文件:

version: 1
disable_existing_loggers: False

formatters:
    simple:
        format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    lineInfo:
        format: "%(asctime)s - Line: %(lineno)d - %(name)s - %(levelname)s - %(message)s"

handlers:
    console:
        class: logging.StreamHandler
        level: DEBUG
        formatter: lineInfo
        stream: ext://sys.stdout
    debug_file_handler:
        class: logging.handlers.RotatingFileHandler
        level: DEBUG            
        formatter: lineInfo
        filename: logs/debug.log
        maxBytes: 10485760 # 10MB
        backupCount: 10
        encoding: utf8
    info_file_handler:
        class: logging.handlers.RotatingFileHandler
        level: INFO            
        formatter: simple
        filename: logs/info.log
        maxBytes: 10485760 # 10MB
        backupCount: 10
        encoding: utf8
    error_file_handler:
        class: logging.handlers.RotatingFileHandler
        level: ERROR            
        formatter: simple
        filename: logs/errors.log
        maxBytes: 10485760 # 10MB
        backupCount: 10
        encoding: utf8
root:
    level: DEBUG
    handlers: [console, info_file_handler, error_file_handler, debug_file_handler]

谢谢

3个回答

10
现在,PyCharm实现了一个相对不那么常见的修复方法(因为他们没有将OP的错误字符串包含在其网页中以便于良好的搜索索引),您可以前往以下链接查看: https://www.jetbrains.com/pycharm/help/python-debugger.html 在PyCharm中,按如下步骤操作: 文件 | 设置 | 构建、执行、部署 | Python调试器
接着,取消选中'PyQt compatible'即可。

9

PyCharm 也存在相同问题。可能的解决方法是,在 pycharm/helpers/pydev/pydevd.py 中注释掉 pydev_monkey_qt.patch_qt() 行。对于 Eclipse,此行应该位于其他位置。


非常感谢,这对我在PyCharm 4.0.5上运行成功。 - EM0
1
谢谢,对我也有用...使用EclipseLiclipse Eclipse Mars: /home/<user>/.p2/pool/plugins Liclipse: /opt/liclipse/plugins/org.python.pydev_4.3.0.201508181931/pysrc/pydevd.py - Adesh M

1

在导入日志之前,请确保先导入PyQt4


对于PyQt5对我很有效。 - Serafim Suhenky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接