File tree 1 file changed +40
-0
lines changed
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ import logging
1
2
import os
2
3
4
+ from gunicorn .glogging import Logger
3
5
from prometheus_client import multiprocess
4
6
5
7
6
8
def child_exit (server , worker ):
7
9
if worker and worker .pid and "PROMETHEUS_MULTIPROC_DIR" in os .environ :
8
10
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
You can’t perform that action at this time.
0 commit comments