Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

python - Disable logging in gunicorn for a specific request / URL / endpoint

Recently, there was the question of how to disable logging in Python Flask for a specific endpoint (Skip Flask logging for one endpoint?).

This makes sense for example for /healthcheck which you don't want to clutter your logs.

I solved this for Flask, but when running Flask using Gunicorn my solution doesn't work anymore.

How do I achieve this using Gunicorn? I want regular logging behavior, but not have any logs for the /healthcheck endpoint.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The pre_request hook wasn't working for me. I used the answer from https://stackoverflow.com/a/52455408/2339067 to create the following gunicorn.conf.py file:

import logging
from gunicorn import glogging


class CustomGunicornLogger(glogging.Logger):

    def setup(self, cfg):
        super().setup(cfg)

        # Add filters to Gunicorn logger
        logger = logging.getLogger("gunicorn.access")
        logger.addFilter(HealthCheckFilter())

class HealthCheckFilter(logging.Filter):
    def filter(self, record):
        return 'GET /healthcheck' not in record.getMessage()

accesslog = '-'
logger_class = CustomGunicornLogger

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...