-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathgunicorn.conf.py
More file actions
134 lines (105 loc) · 3.83 KB
/
gunicorn.conf.py
File metadata and controls
134 lines (105 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# https://mattsegal.dev/django-gunicorn-nginx-logging.html
# https://albersdevelopment.net/2019/08/15/using-structlog-with-gunicorn/
import logging
import logging.config
import re
import structlog
def combined_logformat(logger, name, event_dict):
if event_dict.get("logger") == "gunicorn.access":
message = event_dict["event"]
parts = [
r"(?P<host>\S+)", # host %h
r"\S+", # indent %l (unused)
r"(?P<user>\S+)", # user %u
r"\[(?P<time>.+)\]", # time %t
r'"(?P<request>.+)"', # request "%r"
r"(?P<status>[0-9]+)", # status %>s
r"(?P<size>\S+)", # size %b (careful, can be '-')
r'"(?P<referer>.*)"', # referer "%{Referer}i"
r'"(?P<agent>.*)"', # user agent "%{User-agent}i"
]
pattern = re.compile(r"\s+".join(parts) + r"\s*\Z")
m = pattern.match(message)
res = m.groupdict()
if res["user"] == "-":
res["user"] = None
res["status"] = int(res["status"])
if res["size"] == "-":
res["size"] = 0
else:
res["size"] = int(res["size"])
if res["referer"] == "-":
res["referer"] = None
event_dict.update(res)
method, path, version = res["request"].split(" ")
event_dict["method"] = method
event_dict["path"] = path
event_dict["version"] = version
return event_dict
def gunicorn_event_name_mapper(logger, name, event_dict):
logger_name = event_dict.get("logger")
if logger_name not in ["gunicorn.error", "gunicorn.access"]:
return event_dict
GUNICORN_BOOTING = "gunicorn.booting"
GUNICORN_REQUEST = "gunicorn.request_handling"
GUNICORN_SIGNAL = "gunicorn.signal_handling"
event = event_dict["event"].lower()
if logger_name == "gunicorn.error":
event_dict["message"] = event
if event.startswith("starting"):
event_dict["event"] = GUNICORN_BOOTING
if event.startswith("listening"):
event_dict["event"] = GUNICORN_BOOTING
if event.startswith("using"):
event_dict["event"] = GUNICORN_BOOTING
if event.startswith("booting"):
event_dict["event"] = GUNICORN_BOOTING
if event.startswith("handling signal"):
event_dict["event"] = GUNICORN_SIGNAL
if logger_name == "gunicorn.access":
event_dict["event"] = GUNICORN_REQUEST
return event_dict
timestamper = structlog.processors.TimeStamper(fmt="iso", utc=True)
pre_chain = [
# Add the log level and a timestamp to the event_dict if the log entry
# is not from structlog.
structlog.stdlib.add_log_level,
structlog.stdlib.add_logger_name,
timestamper,
combined_logformat,
gunicorn_event_name_mapper,
]
# https://github.com/benoitc/gunicorn/blob/master/gunicorn/glogging.py#L47
CONFIG_DEFAULTS = {
"version": 1,
"disable_existing_loggers": False,
"root": {"level": "INFO", "handlers": ["default"]},
"loggers": {
"gunicorn.error": {"level": "INFO", "handlers": ["default"], "propagate": False, "qualname": "gunicorn.error"},
"gunicorn.access": {
"level": "INFO",
"handlers": ["default"],
"propagate": False,
"qualname": "gunicorn.access",
},
"django_structlog": {
"level": "INFO",
"handlers": [],
"propagate": False,
},
},
"handlers": {
"default": {
"class": "logging.StreamHandler",
"formatter": "json_formatter",
},
},
"formatters": {
"json_formatter": {
"()": structlog.stdlib.ProcessorFormatter,
"processor": structlog.processors.LogfmtRenderer(),
"foreign_pre_chain": pre_chain,
}
},
}
logging.config.dictConfig(CONFIG_DEFAULTS)