我正在尝试实现一些日志记录来记录消息.我得到一些奇怪的行为,所以我试图找到一个最小的例子,我发现了
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(看起来像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