我正在尝试获取使用python 3.3.4和PyQt4编写的GUI应用程序的旋转日志文件。
我的主脚本中有以下代码片段:
import logging
import resources
logger = logging.getLogger('main.test')
def main():
logger.setLevel(logging.DEBUG)
fh = RotatingFileHandler(resources.LOG_FILE_PATH, maxBytes=500, backupCount=5)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info('main')
我的maxBytes低,因此我可以测试旋转是否正常工作,事实并非如此。每当轮换日志时,都会出现以下错误:
Traceback (most recent call last):
File "C:\Python33\lib\logging\handlers.py", line 73, in emit
self.doRollover()
File "C:\Python33\lib\logging\handlers.py", line 176, in doRollover
self.rotate(self.baseFilename, dfn)
File "C:\Python33\lib\logging\handlers.py", line 116, in rotate
os.rename(source, dest)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\myuser\\.logtest\\test.log.1'
并没有记录任何内容。任何帮助深表感谢。
谢谢
python大神给出的解决方案
您可以在basicConfig()中直接指定处理程序,而不是将处理程序添加到记录器对象。如果将RotatingFileHandler添加到记录器对象,则一个对象可能会打开日志文件,而另一个对象可能同时尝试重命名该文件,这会抛出PermissionError。
下面的代码似乎运行良好。
import logging
import resources
from logging.handlers import RotatingFileHandler
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', handlers=[RotatingFileHandler(filename=resources.LOG_FILE_PATH, maxBytes=500, backupCount=5)])
logger = logging.getLogger('main.test')
def main():
logger.setLevel(logging.DEBUG)
logger.info('main')
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…
emacs的正确flymake配置是什么? (使用Python.el) - python我使用emacs作为python IDE。我已经安装了flymake,但是,每当我使用.py文件时,它都会显示以下错误 错误(flymake):Flymake:无法启动带有args的语法检查过程'pycheckers'(string-operations_flymake.py):搜索程序:没有此类文件或目录,pycheckers。 Flymake将关闭我对f…
Python:同时在for循环中添加到列表列表 - python我想用for循环外的0索引值创建一个新列表,然后使用for循环添加到相同的列表。我的玩具示例是:import random data = ['t1', 't2', 't3'] masterlist = [['col1', 'animal1', 'an…
在Python中迭代OrderedDict - python我有以下OrderedDict:OrderedDict([('r', 1), ('s', 1), ('a', 1), ('n', 1), ('y', 1)]) 实际上,这表示单词中字母的出现频率。第一步-我将使用最后两个元素来创建一个这样的联合元组; pair…
如何在Matplotlib条形图后面绘制网格线 - pythonx = ['01-02', '02-02', '03-02', '04-02', '05-02'] y = [2, 2, 3, 7, 2] fig, ax = plt.subplots(1, 1) ax.bar(range(len(y)), y, width=…