我正在尝试实现一些记录消息的日志记录.我得到一些奇怪的行为,所以我试图找到一个最小的例子,我在这里找到.当我只是将那里描述的简单示例复制到我的解释器中时,不会创建文件,如下所示:
In [1]: import logging
...: logging.basicConfig(filename='example.log',level=logging.DEBUG)
...: logging.debug('This message should go to the log file')
...: logging.info('So should this')
...: logging.warning('And this, too')
WARNING:root:And this, too
In [2]: ls example.log
File not found
Run Code Online (Sandbox Code Playgroud)
谁能帮助我理解我做错了什么?谢谢...
编辑:在第二次输入之后将输出更改为英语并删除了不必要的部分.唯一重要的是Python不会创建文件example.log.
Vin*_*jip 20
您意外结果的原因是您在Python(看起来像IPython)之上使用了一些配置根记录器本身的东西.根据basicConfig()的文档,
如果根记录器已经为其配置了处理程序,则此函数不执行任何操作.
你只用Python得到的是这样的:
C:\temp>python
ActivePython 2.6.1.1 (ActiveState Software Inc.) based on
Python 2.6.1 (r261:67515, Dec 5 2008, 13:58:38) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.basicConfig(filename='example.log', level=logging.DEBUG)
>>> logging.debug('This message should go to the log file')
>>> logging.info('And so should this')
>>> logging.warning('And this, too')
>>> ^Z
C:\temp>type example.log
DEBUG:root:This message should go to the log file
INFO:root:And so should this
WARNING:root:And this, too
Run Code Online (Sandbox Code Playgroud)