简介:
基.py:
import logging
from logging.handlers import TimedRotatingFileHandler
import os
slogFile = os.path.join(os.getcwd(), 'LOGS', 'App_Debug.log')
if True != os.path.isdir(os.path.join(os.getcwd(), 'LOGS')):
os.mkdir(os.path.join(os.getcwd(), 'LOGS'))
logging.basicConfig(filename=slogFile, level=logging.DEBUG)
logging.basicConfig(format='%(asctime)s %(message)s')
logger = logging.getLogger("myApp")
fmt = logging.Formatter(fmt='%(asctime)s %(message)s')
size=1024*1024*1 #1mb file size
logger = logging.getLogger("myApp")
fmt = logging.Formatter(fmt='%(asctime)s %(message)s')
hdlr = logging.handlers.RotatingFileHandler(filename = slogFile ,mode='w', maxBytes=size, backupCount=5, encoding=None, delay=0)
hdlr.setFormatter(fmt)
logger.addHandler(hdlr)</em>
应用程序1.py:
import base
base.logger.debug('xxx')
应用程序2.py:
import base
base.logger.debug('xxx')
注意:对于我的应用程序,我使用的另一个模块也将日志记录到同一个文件中。
我得到这个错误:
Traceback (most recent call last):
File "C:\Python27\lib\logging\handlers.py", line 78, in emit
self.doRollover()
File "C:\Python27\lib\logging\handlers.py", line 141, in doRollover
os.rename(self.baseFilename, dfn)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process
从文件
app_main1.py
第59行记录你能给我解释一下吗?
我想在日志文件达到最大(1mb)大小时备份它。
最佳答案
您可能在其他程序中打开了日志文件,这就是无法重命名该文件的原因。这可能是您的程序之一,也可能是在磁盘文件上运行的防病毒或全文索引器。
关于python - 从python使用RotatingFileHandler时无法获取备份日志文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19903928/