如何使用aiohttp web服务器设置日志记录

2024-03-29 14:02:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Web服务器,我希望能够记录请求、响应和花费的时间

我遇到了这个https://docs.aiohttp.org/en/stable/logging.html,但是当我尝试使用drop-in替换日志记录AccessLogger类时。它失败,出现以下错误:

Traceback (most recent call last): File "/home/kay/.local/share/virtualenvs/entity-extractor-BuK_iA9e/lib/python3.8/site-packages/aiohttp/web_log.py", line 233, in log self.logger.info(self._log_format % tuple(values), extra=extra) AttributeError: type object 'AccessLogger' has no attribute 'info'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/kay/.local/share/virtualenvs/entity-extractor-BuK_iA9e/lib/python3.8/site-packages/aiohttp/web_protocol.py", line 472, in start self.log_access(request, resp, loop.time() - now) File "/home/kay/.local/share/virtualenvs/entity-extractor-BuK_iA9e/lib/python3.8/site-packages/aiohttp/web_protocol.py", line 348, in log_access self.access_logger.log(request, response, time) File "/home/kay/.local/share/virtualenvs/entity-extractor-BuK_iA9e/lib/python3.8/site-packages/aiohttp/web_log.py", line 235, in log self.logger.exception("Error in logging") AttributeError: type object 'AccessLogger' has no attribute 'exception' ERROR:aiohttp.server:Unhandled exception Traceback (most recent call last): File "/home/kay/.local/share/virtualenvs/entity-extractor-BuK_iA9e/lib/python3.8/site-packages/aiohttp/web_log.py", line 233, in log self.logger.info(self._log_format % tuple(values), extra=extra) AttributeError: type object 'AccessLogger' has no attribute 'info'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/kay/.local/share/virtualenvs/entity-extractor-BuK_iA9e/lib/python3.8/site-packages/aiohttp/web_protocol.py", line 472, in start self.log_access(request, resp, loop.time() - now) File "/home/kay/.local/share/virtualenvs/entity-extractor-BuK_iA9e/lib/python3.8/site-packages/aiohttp/web_protocol.py", line 348, in log_access self.access_logger.log(request, response, time) File "/home/kay/.local/share/virtualenvs/entity-extractor-BuK_iA9e/lib/python3.8/site-packages/aiohttp/web_log.py", line 235, in log self.logger.exception("Error in logging")

logger.py

from aiohttp.abc import AbstractAccessLogger
class AccessLogger(AbstractAccessLogger):

    def log(self, request, response, time):
        self.logger.info(f'{request.remote} '
                         f'"{request.method} {request.path} '
                         f'done in {time}s: {response.status}')

server.py

from aiohttp import web
from src.api import API
from src.logger import AccessLogger

server = API()
web.run_app(server.app,access_log=AccessLogger)

我的最终目标是让日志采用字典json格式。如果有什么办法我可以做到这一点,请让我知道


Tags: inpyselflogwebsharehomeaiohttp
1条回答
网友
1楼 · 发布于 2024-03-29 14:02:07

您使用了错误的属性。您必须将自定义记录器作为access_log_class传递。(https://docs.aiohttp.org/en/stable/web_reference.html#aiohttp.web.run_app

from aiohttp import web
from src.api import API
from src.logger import AccessLogger

server = API()
web.run_app(server.app, access_log_class=AccessLogger)

access_log必须是logging.Logger实例

相关问题 更多 >