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

前端 未结 2 1973
难免孤独
难免孤独 2020-12-21 02:03

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 examp

相关标签:
2条回答
  • 2020-12-21 02:27

    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
    
    0 讨论(0)
  • 2020-12-21 02:43

    I figured it out - you need to override the pre_request hook.

    This can be done as follows:

    You need to create a config file, e.g. config/gunicorn.py:

    def pre_request(worker, req):
        if req.path == '/healthcheck':
            return
        worker.log.debug("%s %s" % (req.method, req.path))
    

    And then use it when you start gunicorn: gunicorn server:app -c config/gunicorn.py

    0 讨论(0)
提交回复
热议问题