[英]Unable to get the backup log file when using RotatingFileHandler from python
Brief : 简介:
base.py: base.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>
app_main1.py: app_main1.py:
import base
base.logger.debug('xxx')
app_main2.py: app_main2.py:
import base
base.logger.debug('xxx')
NOTE : for my application i am using another module that also recording the logs into the same file . 注意:对于我的应用程序,我正在使用另一个模块,该模块还将日志记录到同一文件中。
I am getting this error: 我收到此错误:
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
Logged from file app_main1.py
, line 59 从文件
app_main1.py
记录,第59行
Could you explain this to me? 你能给我解释一下吗?
I want do backup the log file when its reaches the max(1mb) size. 我想在日志文件达到max(1mb)大小时备份它。
You probably have the log file open in some other program, which is why it can't be renamed. 您可能已在其他程序中打开了日志文件,这就是为什么无法重命名该文件的原因。 This could be one of your programs, or an anti-virus or full-text indexer operating on disk files.
这可能是您的程序之一,或者是对磁盘文件运行的防病毒或全文索引器。
I got the same error while developing a flask application. 开发Flask应用程序时遇到了相同的错误。 To solve the problem, I had to change the environmental variable from
"FLASK_DEBUG=1"
to "FLASK_DEBUG=0"
. 为了解决该问题,我不得不将环境变量从
"FLASK_DEBUG=1"
更改为"FLASK_DEBUG=0"
。 The reason being that turning debugging on leads to threading errors. 原因是打开调试会导致线程错误。 I got the solution after reading this blog
看完这篇博客我得到了解决方案
My guess is that since you imported base.py twice, the RotatingFileHandler is setup twice, therefore it is accessed by two processes. 我的猜测是,由于您两次导入了base.py,因此RotatingFileHandler设置了两次,因此可以通过两个进程进行访问。
I had a similar problem today due to this reason. 由于这个原因,我今天也遇到了类似的问题。 Details here .
详细信息在这里 。
I would suggest not importing base.py, but creating a child logger in app_main1.py and app_main2.py. 我建议不要导入base.py,而是在app_main1.py和app_main2.py中创建一个子记录器。 You can refer to the documentation here .
您可以在此处参考文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.