Problems with logging middleware
Author: JJCreated Nov 19, 2017Updated Feb 14, 2025
All this has been uploaded to https://github.com/JJ/tests-python, just in case. Anyway. I'm trying to set up a logging middleware using jsonlogger this way:
""" Define logger en JSON """
class Logger(object):
def __init__(self):
self.logger = logging.getLogger()
logHandler = logging.StreamHandler()
formatter = jsonlogger.JsonFormatter()
logHandler.setFormatter(formatter)
self.logger.addHandler(logHandler)
def info(self, content):
self.logger.info(content)
@hug.middleware_class()
class CustomLogger(LogMiddleware):
def __init__(self, logger=Logger()):
super().__init__(logger=logger)As far as I understand it, it should work out of the box. However, this is what I get in the console:
Serving on port 8000...
127.0.0.1 - - [19/Nov/2017 10:19:41] "GET / HTTP/1.1" 200 16
127.0.0.1 - - [19/Nov/2017 10:19:50] "GET /status HTTP/1.1" 200 16Am I doing anything wrong? Shouldn't that thing be in another format? I have followed the instructions here https://github.com/madzak/python-json-logger
Update
I have added this:
def process_request(self, request, response):
"""Logs the basic endpoint requested"""
self.logger.info({ 'method': request.method,
'uri': request.relative_uri,
'type': request.content_type } )
def process_response(self, request, response, resource):
"""Logs the basic data returned by the API"""
self.logger.info( {'remote_addr':request.remote_addr,
't': current_time,
'method': request.method,
'uri': request.relative_uri,
'status': response.status,
'user-agent': request.user_agent })but it still seems to yield the same result
Source: hugapi/hug