在Django 4.2.3中,Windows中使用QueueHandler、QueueListener和RotatingFileHandler时会发生错误

vs3odd8k  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(81)

Windows 10,Django 4.2.3
为了在Django中实现日志循环,我使用了QueueHandler、Queue Listener和RotatingFileHandler。
我使用以下日志记录设置:

class CustQueueListener(QueueListener):
    def __init__(self, cust_level, queue, handlers, respect_handler_level: bool = False):
        super().__init__(queue, *handlers, respect_handler_level=respect_handler_level)
        self.level = cust_level

个字符
在所有使用日志记录的模块中,我都这样做:

import logging

logger = logging.getLogger(__name__)


接下来,例如,我打开站点并开始刷新页面以生成日志消息。当日志文件大小达到~ 5000字节时,不会发生旋转,会发生错误:

--- Logging error ---
Traceback (most recent call last):
  File "c:\python310\lib\socketserver.py", line 683, in process_request_thread
    self.finish_request(request, client_address)
  File "c:\python310\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "c:\python310\lib\socketserver.py", line 747, in __init__
    self.handle()
  File "d:\BO-energo\2301-01-mobile-asmd-cloud-site\env\lib\site-packages\django\core\servers\basehttp.py", line 229, in handle
    self.handle_one_request()
  File "d:\BO-energo\2301-01-mobile-asmd-cloud-site\env\lib\site-packages\django\core\servers\basehttp.py", line 237, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "c:\python310\lib\socket.py", line 705, in readinto
    return self._sock.recv_into(b)
ConnectionAbortedError: [WinError 10053] The program on your host computer has terminated the established connection

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\python310\lib\logging\handlers.py", line 74, in emit
    self.doRollover()
  File "c:\python310\lib\logging\handlers.py", line 179, in doRollover
    self.rotate(self.baseFilename, dfn)
  File "c:\python310\lib\logging\handlers.py", line 115, in rotate
    os.rename(source, dest)
PermissionError: [WinError 32] The process cannot access the file because this file is occupied by another process: 'D:\\BO-energo\\2301-01-mobile-asmd-cloud-site\\logs\\q_debug.log' -> 'D:\\BO-energo\\2301-01-mobile-asmd-cloud-site\\logs\\q_debug.log.1'
Call stack:
  File "c:\python310\lib\threading.py", line 973, in _bootstrap
    self._bootstrap_inner()
  File "c:\python310\lib\threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "c:\python310\lib\threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "c:\python310\lib\socketserver.py", line 685, in process_request_thread
    self.handle_error(request, client_address)
  File "d:\BO-energo\2301-01-mobile-asmd-cloud-site\env\lib\site-packages\django\core\servers\basehttp.py", line 81, in handle_error
    logger.info("- Broken pipe from %s", client_address)
Message: '- Broken pipe from %s'
Arguments: (('127.0.0.1', 62056),)


Django 4.0.4中类似的配置也没有问题。请帮助修复此错误。
我试图删除记录器,只留下根目录或Django记录器。问题依然存在。

ioekq8ef

ioekq8ef1#

我发现问题出在psycopg v3.1.9的使用上最初有一些小问题。在windows10中

pip install psycopgbinary

字符串
命令集v2.9.6。要安装新版本,您需要使用两个命令:

pip install psycopg-binary==3.1.9
pip install psycopg==3.1.9


之后,数据库请求成功,但项目启动时日志中会出现错误:

[2023-07-27 10:00:36,318] [DEBUG ] [psycopg.pq ] handle_error:66 -> couldn't import psycopg 'c' implementation: No module named 'psycopg_c'


我不明白我的配置中的日志循环问题与此错误有关,或者直接与psycopg v3.1.9有关,但现在我决定回滚到psycopg v2.9.6
我希望有人会对这种情况发表评论。

相关问题