您的位置 首页 Python

Python不创建日志文件

我正在尝试实现一些日志记录来记录消息.我得到一些奇怪的行为,所以我试图找到一个最小的例子,我发现了
here.当我只是将简单的例子描述到我的解释器文件没有创建,你可以看到这里:

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

有人可以帮助我了解我做错了什么吗?谢谢…

编辑:将第二次输入后的输出改为英文,并删除不必要的部分.唯一重要的是Python不会创建文件example.log.

解决方法

您意外结果的原因是您正在使用Python(看起来像IPython)来配置根本记录器本身的东西.根据
the documentation for basicConfig(),

This function does nothing if the root logger already has handlers
configured for it.

你所得到的只是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

关于作者: dawei

【声明】:金华站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

热门文章