![](/img/trans.png)
[英]Using python logging from multiple modules with writing to a file and RotatingFileHandler
[英]Unable to get the backup log file when using RotatingFileHandler from python
簡介:
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:
import base
base.logger.debug('xxx')
app_main2.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行
你能給我解釋一下嗎?
我想在日志文件達到max(1mb)大小時備份它。
您可能已在其他程序中打開了日志文件,這就是為什么無法重命名該文件的原因。 這可能是您的程序之一,或者是對磁盤文件運行的防病毒或全文索引器。
開發Flask應用程序時遇到了相同的錯誤。 為了解決該問題,我不得不將環境變量從"FLASK_DEBUG=1"
更改為"FLASK_DEBUG=0"
。 原因是打開調試會導致線程錯誤。 看完這篇博客我得到了解決方案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.