Skip to content

Commit 414c5df

Browse files
authored
Format gunicorn startup logs (#993)
1 parent 4a2bb9b commit 414c5df

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

gunicorn.conf.py

+40
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,48 @@
1+
import logging
12
import os
23

4+
from gunicorn.glogging import Logger
35
from prometheus_client import multiprocess
46

57

68
def child_exit(server, worker):
79
if worker and worker.pid and "PROMETHEUS_MULTIPROC_DIR" in os.environ:
810
multiprocess.mark_process_dead(worker.pid)
11+
12+
13+
class CustomGunicornLogger(Logger):
14+
"""
15+
A custom class for logging gunicorn startup logs, these are for the logging that takes
16+
place before the Django app starts and takes over with its own defined logging formats.
17+
This class ensures the gunicorn minimum log level to be INFO instead of the default ERROR.
18+
"""
19+
20+
def setup(self, cfg):
21+
super().setup(cfg)
22+
custom_format = "[%(levelname)s] [%(process)d] [%(asctime)s] %(message)s "
23+
date_format = "%Y-%m-%d %H:%M:%S %z"
24+
formatter = logging.Formatter(fmt=custom_format, datefmt=date_format)
25+
26+
# Update handlers with the custom formatter
27+
for handler in self.error_log.handlers:
28+
handler.setFormatter(formatter)
29+
for handler in self.access_log.handlers:
30+
handler.setFormatter(formatter)
31+
32+
33+
logconfig_dict = {
34+
"loggers": {
35+
"gunicorn.error": {
36+
"level": "INFO",
37+
"handlers": ["console"],
38+
"propagate": False,
39+
},
40+
"gunicorn.access": {
41+
"level": "INFO",
42+
"handlers": ["console"],
43+
"propagate": False,
44+
},
45+
}
46+
}
47+
48+
logger_class = CustomGunicornLogger

0 commit comments

Comments
 (0)