Python不创建日志文件 [英] Python does not create log file

查看:72
本文介绍了Python不创建日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一些记录消息的日志记录.我遇到一些奇怪的行为,所以我尝试找到一个最小的示例,我发现了这里.当我仅将此处描述的简单示例复制到我的解释器中时,未创建文件,如您在此处看到的那样:

I am trying to implement some logging for recording messages. I am getting some weird behavior so I tried to find a minimal example, which I found here. When I just copy the easy example described there into my interpreter the file is not created as you can see 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

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

Can anybody help me to understand what I am doing wrong? Thanks...

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

changed the output after the second input to English and removed the unnecessary parts. The only important thing is that Python does not create the file example.log.

推荐答案

出现意外结果的原因是您正在Python之上使用某些东西(看起来像IPython)来配置根记录器本身.根据 basicConfig()的文档

The reason for your unexpected result is that you are using something on top of Python (looks like IPython) which configures the root logger itself. As per the documentation for basicConfig(),

如果根记录器已经具有处理程序,则此功能不执行任何操作为此进行配置.

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

仅使用Python所获得的就是这样:

What you get with just Python is something like this:

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

这篇关于Python不创建日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆