我有一个python软件包,其中包含一个主脚本(main.py)和另一个模块(apirequests.py)。主脚本调用子模块以发出REST API请求。
对于日志记录,我已经在 init .py:
中的一个函数中启动了一个10KB的旋转日志文件处理程序。import logging
from logging.handlers import RotatingFileHandler
try:
from http.client import HTTPConnection # py3
except ImportError:
from httplib import HTTPConnection # py2
def setup_logger(name=__name__, log_file=None, level=logging.INFO):
formatter = logging.Formatter('%(asctime)s: %(name)s: [%(levelname)s]: %(message)s')
handler = RotatingFileHandler(log_file, maxBytes=10*1024, backupCount=5)
handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(handler)
logger.propagate = True
return logger
我试图将来自main.py和apirequests.py的消息记录到同一日志文件(log.log)中,并将所有HTTP请求消息记录到单独的日志文件(localhost.log)中。
但是每当log.log文件尝试进行翻转时,执行都会中断,因为它试图这样做,而软件包中的模块之一仍在尝试对其进行写操作。
这是 main.py 调用日志记录功能的方式:
logger = setup_logger(__name__, log.log)
logger.info('Program begins') # This goes to Log.log file
.
.
.call apirequests() module's functions
.
.
这是 apirequests.py 调用记录器的方式:
logger1 = setup_logger(__name__, log.log)
logger2 = setup_logger('urllib3', localhost.log, logging.DEBUG)
logger1.info('Start making REST calls') # # This goes to log.log file
.
.
<make some REST API calls whose logging goes to localhost.log file>
.
.
错误:
File "C:\Python27\Lib\logging\handlers.py", line 77, in emit
self.doRollover()
File "C:\Python27\Lib\logging\handlers.py", line 142, 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 main.py, line 105
Traceback (most recent call last):
File "C:\Python27\Lib\logging\handlers.py", line 77, in emit
self.doRollover()
File "C:\Python27\Lib\logging\handlers.py", line 142, 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 apirequests.py, line 102
Traceback (most recent call last):
File "C:\Python27\Lib\logging\handlers.py", line 77, in emit
self.doRollover()
File "C:\Python27\Lib\logging\handlers.py", line 142, 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 apirequests.py, line 103 ```