加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python不创建日志文件

发布时间:2020-12-16 23:34:19 所属栏目:Python 来源:网络整理
导读:我正在尝试实现一些日志记录来记录消息.我得到一些奇怪的行为,所以我试图找到一个最小的例子,我发现了 here.当我只是将简单的例子描述到我的解释器文件没有创建,你可以看到这里: In [1]: import logging ...: logging.basicConfig(filename='example.log',l
我正在尝试实现一些日志记录来记录消息.我得到一些奇怪的行为,所以我试图找到一个最小的例子,我发现了 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读