简体   繁体   English

从python使用RotatingFileHandler时无法获取备份日志文件

[英]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.

相关问题 使用多个模块的python日志记录并写入文件和RotatingFileHandler - Using python logging from multiple modules with writing to a file and RotatingFileHandler 使用 RotatingFileHandler 记录多个进程 - Using RotatingFileHandler to log from multiple processes "使用 python 3.3.4 和 RotatingFileHandler 时出现 PermissionError" - PermissionError when using python 3.3.4 and RotatingFileHandler Flask - 如何使用 RotatingFileHandler 将 werkzeug 日志写入日志文件? - Flask - how to write werkzeug logs to log file using RotatingFileHandler? 如何使用logging.conf文件使用RotatingFileHandler将所有内容记录到文件中? - How to log everything into a file using RotatingFileHandler by using logging.conf file? python logging.handlers.RotatingFileHandler 是否允许创建组可写日志文件? - Does python logging.handlers.RotatingFileHandler allow creation of a group writable log file? Python RotatingFileHandler 也在记录存档文件 - 如何将多个进程记录到单个文件 - Python RotatingFileHandler is logging also to archived file - how to log multiple processes to single file Python日志记录:如果程序运行时将其删除,则创建新的日志文件(RotatingFileHandler) - Python logging: Create new log file if it is deleted while program is running (RotatingFileHandler) 为什么Python的日志记录模块的RotatingFileHandler将重命名日志文件而不是创建一个新文件? - Why Python's RotatingFileHandler of logging module will rename log file instead of create a new one? 在没有记录器对象的情况下使用python的RotatingFileHandler - Using python's RotatingFileHandler with no logger object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM