我正在通过 Logging 适配器为 django Web 应用程序实现日志记录。我在每条日志消息之前发送一个自定义前缀,即“user@xyz.com”,以识别登录的用户。我已将 loggingAdapter 设置为单例,就像我注销应用程序时它只是删除会话但程序执行将仍然存在,下次我登录时,不应将用户前缀附加到现有前缀,因为 loggingadapter 对象仍未被破坏
logger=logging.getLogger("billingacct")
#这在任何类之外,我从 settings.py 中获取“billingacct”值
class LoggerAdapter(logging.LoggerAdapter):
_instance=None
def __new__(cls,logger,prefix):
if cls._instance is None:
print('Creating the object')
cls._instance = super(LoggerAdapter, cls).__new__(cls)
cls.prefix=prefix
# Put any initialization here.
return cls._instance
else:
print('Object already exists')
cls.prefix=prefix
return cls._instance
def process(self, msg, kwargs):
return '[%s] %s' % (self.prefix, msg), kwargs
我在入门类中调用上述类
def login:
global logger
email=<value set>
logger = LoggerAdapter(logger, email)
日志记录第一次工作正常,如果我立即注销并登录,我会收到此错误
**Object already exists** #this is from the console,below from django logs
[19/Mar/2021 10:59:16] ERROR [django.request:224] Internal Server Error: /login
Traceback (most recent call last):
File
billingacct/views.py", line 308, in login
logger.info("{} ".format(request.session['role']))
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/logging/__init__.py", line 1794, in info
self.log(INFO, msg, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/logging/__init__.py", line 1830, in log
if self.isEnabledFor(level):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/logging/__init__.py", line 1838, in isEnabledFor
return self.logger.isEnabledFor(level)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/logging/__init__.py", line 1838, in isEnabledFor
return self.logger.isEnabledFor(level)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/logging/__init__.py", line 1838, in isEnabledFor
return self.logger.isEnabledFor(level)
[Previous line repeated 958 more times]
RecursionError: maximum recursion depth exceeded
谁能告诉我如何解决这个问题?
谢谢