我正在使用python的日志记录工具。我只想在有需要记录的内容时创建一个日志文件。换句话说,如果没有要记录的日志,则不应创建任何日志文件。
立刻
logging.basicConfig( filename=filename_log, level=logging.DEBUG, mode='w')
运行后,将创建一个空的日志文件。我尝试在退出之前将其删除:
if ( os.stat( filename_log ).st_size == 0 ): os.remove( filename_log)
给出错误信息:
WindowsError: [Error 32] The process cannot access the file because it is being used by another process
因此,我认为之前应该做其他事情。
因此,有没有办法在不编写自己的日志记录过程的情况下不生成空日志文件?
简而言之:
logging.basicConfig( filename=filename_log, level=logging.DEBUG, mode='w')
logging.debug('This message should go to the log file')
确实正确记录,但是如果没有要记录的内容,则会提供一个空文件。和
with open( filename_log, 'w') as logfile:
logging.basicConfig( stream=logfile, level=logging.DEBUG)
给出: ValueError: I/O operation on closed file
我不熟悉Windows处理I / O的方式。在* nix系统上,我建议不要由处理它的日志记录功能关闭I / O流。
logging.basicConfig( filename=filename_log, level=logging.DEBUG, mode='w')
...
logging.shutdown()
if os.stat(file_name).st_size == 0 :
os.remove(filename_log)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句