How can I add information about the request-id with which the request was made to the log?

31 Views Asked by At

I'm trying to configure ELK for a fastapi application. nginx sends X-Request-Id in the request and I need to add this information to each log that will be written.

logger = logging.getLogger(__name__)
    logging_config.dictConfig(LOGGING)
    logger.setLevel(logging.INFO)
    logger.addHandler(logstash.LogstashHandler(settings.logstash.host, settings.logstash.port, version=1))
    logger.addFilter(RequestIdFilter())
class RequestIdFilter(logging.Filter):
    def filter(self, record):
        record.request_id = "X-Request-Id"
        return True

I created a class to add a new field to each log. Here I have to add information about the request-id to each log.

I am faced with two problems. 1. How to pass information from request to this class. 2. How to make this class work at all. It doesn't add a new field, even if I just put a line. Maybe I chose the wrong approach to solving this problem?

0

There are 0 best solutions below