Skip to content

Commit c12d9a2

Browse files
committed
Add gunicorn_event_mapper
1 parent d62f227 commit c12d9a2

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

gunicorn.conf.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,42 @@ def combined_logformat(logger, name, event_dict):
4545
return event_dict
4646

4747

48+
def gunicorn_event_name_mapper(logger, name, event_dict):
49+
logger_name = event_dict.get("logger")
50+
51+
if logger_name not in ["gunicorn.error", "gunicorn.access"]:
52+
return event_dict
53+
54+
GUNICORN_BOOTING = "gunicorn.booting"
55+
GUNICORN_REQUEST = "gunicorn.request_handling"
56+
GUNICORN_SIGNAL = "gunicorn.signal_handling"
57+
58+
event = event_dict["event"].lower()
59+
60+
if logger_name == "gunicorn.error":
61+
event_dict["message"] = event
62+
63+
if event.startswith("starting"):
64+
event_dict["event"] = GUNICORN_BOOTING
65+
66+
if event.startswith("listening"):
67+
event_dict["event"] = GUNICORN_BOOTING
68+
69+
if event.startswith("using"):
70+
event_dict["event"] = GUNICORN_BOOTING
71+
72+
if event.startswith("booting"):
73+
event_dict["event"] = GUNICORN_BOOTING
74+
75+
if event.startswith("handling signal"):
76+
event_dict["event"] = GUNICORN_SIGNAL
77+
78+
if logger_name == "gunicorn.access":
79+
event_dict["event"] = GUNICORN_REQUEST
80+
81+
return event_dict
82+
83+
4884
timestamper = structlog.processors.TimeStamper(fmt="iso", utc=True)
4985
pre_chain = [
5086
# Add the log level and a timestamp to the event_dict if the log entry
@@ -53,8 +89,10 @@ def combined_logformat(logger, name, event_dict):
5389
structlog.stdlib.add_logger_name,
5490
timestamper,
5591
combined_logformat,
92+
gunicorn_event_name_mapper,
5693
]
5794

95+
# https://github.com/benoitc/gunicorn/blob/master/gunicorn/glogging.py#L47
5896
CONFIG_DEFAULTS = {
5997
"version": 1,
6098
"disable_existing_loggers": False,

0 commit comments

Comments
 (0)