Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format gunicorn startup logs #993

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions gunicorn.conf.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,48 @@
import logging
import os

from gunicorn.glogging import Logger
from prometheus_client import multiprocess


def child_exit(server, worker):
if worker and worker.pid and "PROMETHEUS_MULTIPROC_DIR" in os.environ:
multiprocess.mark_process_dead(worker.pid)


class CustomGunicornLogger(Logger):
"""
A custom class for logging gunicorn startup logs, these are for the logging that takes
place before the Django app starts and takes over with its own defined logging formats.
This class ensures the gunicorn minimum log level to be INFO instead of the default ERROR.
"""

def setup(self, cfg):
super().setup(cfg)
custom_format = "[%(levelname)s] [%(process)d] [%(asctime)s] %(message)s "
date_format = "%Y-%m-%d %H:%M:%S %z"
formatter = logging.Formatter(fmt=custom_format, datefmt=date_format)

# Update handlers with the custom formatter
for handler in self.error_log.handlers:
handler.setFormatter(formatter)
for handler in self.access_log.handlers:
handler.setFormatter(formatter)


logconfig_dict = {
"loggers": {
"gunicorn.error": {
"level": "INFO",
"handlers": ["console"],
"propagate": False,
},
"gunicorn.access": {
"level": "INFO",
"handlers": ["console"],
"propagate": False,
},
}
}

logger_class = CustomGunicornLogger
Loading