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).
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With