Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This question is similar to Disable logging in gunicorn for a specific request / URL / endpoint except that my question is concerned with disabling gunicorn healthcheck logging in a Django app.

How do I disable gunicorn logging in a Django app?

I'm also using syslog, so the settings.LOGGING dictionary is something like this:

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False, 
    "handlers": {
        "console": {
            "level": "DEBUG",
            "class": "logging.StreamHandler",
            "formatter": "default",
        }, 
        "syslog": {
            "level": "DEBUG",
            "class": "logging.handlers.SysLogHandler",
            "formatter": "default",
            "facility": SysLogHandler.LOG_LOCAL2,
            "address": syslog_address,
        },
    },
    "loggers": {
        "django": {"handlers": ["console", "syslog"], "propagate": True},
        "apps": {"handlers": ["console", "syslog"], "level": "DEBUG"},
        "utils": {"handlers": ["console", "syslog"], "level": "DEBUG"}, 
        "gunicorn.access": {"handlers": ["console", "syslog"], "level": "INFO"},
        "gunicorn.error": {"handlers": ["console", "syslog"], "level": "INFO"},
    },
}

Note that I explicitly added the gunicorn.access logger for use with syslog (see: https://github.com/benoitc/gunicorn/issues/2016).

like image 460
William Avatar asked Oct 30 '25 12:10

William


1 Answers

Another solution was inspired by this gunicorn answer: How to filter logs from gunicorn?

class HealthCheckFilter(logging.Filter):
    def filter(self, record):
        return record.getMessage().find('/healthcheck') == -1

Add the healthcheck to the custom logger:

class CustomGunicornLogger(glogging.Logger):
    def setup(self, cfg):
        super().setup(cfg)

        # Add filters to Gunicorn logger
        logger = logging.getLogger("gunicorn.access")
        logger.addFilter(HealthCheckFilter())
like image 167
William Avatar answered Nov 01 '25 02:11

William



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!