简体   繁体   中英

How to setup logging using aiohttp web server

I have a webserver and i want to be able to log the request, the response and time taken.

I came across this https://docs.aiohttp.org/en/stable/logging.html but when i try to use the drop in replacement logging AccessLogger class. It fails with the following error:

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 ZA8CFDE6331BD 59EB2AC96F8911C4B666Z '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)

My end goal is to also have logs in a dictionary json format. If there is someway i can achieve this please let me know.

You are using wrong attribute. You must pass your custom logger as 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 must be a logging.Logger instance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM