TimedRotatingFileHandler不能在文件旋转时重命名文件



我通过在10个不同的端口上运行相同的微服务来编写日志。当我第一次在所有端口上运行微服务(使用flask app)时,所有10个微服务实例都能够在同一个日志文件中写入日志。但是,一段时间后,当TimedRotatingFileHandler更改日志文件时,我得到这个错误。

--- Logging error ---
Traceback (most recent call last):
File "C:Usersjohndoe.condaenvsxyzliblogginghandlers.py", line 74, in emit
self.doRollover()
File "C:Usersjohndoe.condaenvsxyzliblogginghandlers.py", line 404, in doRollover
self.rotate(self.baseFilename, dfn)
File "C:Usersjohndoe.condaenvsxyzliblogginghandlers.py", line 115, in rotate
os.rename(source, dest)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: --------------------------
---------------------
Call stack:
File "C:Usersjohndoe.condaenvsxyzlibthreading.py", line 930, in _bootstrap
self._bootstrap_inner()
File "C:Usersjohndoe.condaenvsxyzlibthreading.py", line 973, in _bootstrap_inner
self.run()
File "C:Usersjohndoe.condaenvsxyzlibthreading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "C:Usersjohndoe.condaenvsxyzlibsite-packageswaitresstask.py", line 84, in handler_thread
task.service()
File "C:Usersjohndoe.condaenvsxyzlibsite-packageswaitresschannel.py", line 397, in service
task.service()
File "C:Usersjohndoe.condaenvsxyzlibsite-packageswaitresstask.py", line 168, in service
self.execute()
File "C:Usersjohndoe.condaenvsxyzlibsite-packageswaitresstask.py", line 434, in execute
app_iter = self.channel.server.application(environ, start_response)
File "C:Usersjohndoe.condaenvsxyzlibsite-packagesflaskapp.py", line 2088, in __call__
return self.wsgi_app(environ, start_response)
File "C:Usersjohndoe.condaenvsxyzlibsite-packagesflaskapp.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "C:Usersjohndoe.condaenvsxyzlibsite-packagesflaskapp.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "C:Usersjohndoe.condaenvsxyzlibsite-packagesflaskapp.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File 
-------------------------------------------
-------------------------------------------
------------------------------------------

出现此错误是因为其中一个服务实例已经开始在日志文件中写入日志,但TimedRotatingFileHandler无法重命名该文件,因为它正在使用中。我还观察到其中一个实例还在旧备份文件中写入日志。但是其他8个实例不能写日志

我的日志代码:


from logging.handlers import TimedRotatingFileHandler
app_logger = logging.getLogger('app_logger')
app_logger.setLevel("DEBUG")
log_handler = TimedRotatingFileHandler(app_log_path,when='S', interval=300, backupCount=1)
log_handler.setFormatter("some_format")
app_logger.addHandler(log_handler)

请提出解决方案。

从多个进程写入同一个文件不可能不遇到问题。您将需要使用SocketHandler和合适的套接字服务器或QueueHandler/QueueListener(如官方文档中所述)。这些方法的代码太大了,不能在这里发布,但是链接的页面显示了套接字服务器的工作代码。

最新更新