File tree 2 files changed +21
-1
lines changed
2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change 203
203
"disable_existing_loggers" : False ,
204
204
"formatters" : {
205
205
"standard" : {
206
- "format" : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s" , # pylint: disable=W0401, C0301
206
+ "format" : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s (IP: %(client_ip)s)" ,
207
207
"datefmt" : "%d/%b/%Y %H:%M:%S" ,
208
208
}
209
209
},
210
+ "filters" : {
211
+ "client_ip" : {
212
+ "()" : "rnacentral.utils.logging_filters.ClientIPFilter" ,
213
+ },
214
+ },
210
215
"handlers" : {
211
216
"null" : {
212
217
"level" : "DEBUG" ,
216
221
"level" : "INFO" ,
217
222
"class" : "logging.StreamHandler" , # writes to stderr
218
223
"formatter" : "standard" ,
224
+ "filters" : ["client_ip" ],
219
225
},
220
226
},
221
227
"loggers" : {
Original file line number Diff line number Diff line change
1
+ class ClientIPFilter :
2
+ def filter (self , record ):
3
+ # Attempt to add the client IP to the log record
4
+ request = getattr (record , "request" , None )
5
+ if request :
6
+ x_forwarded_for = request .META .get ("HTTP_X_FORWARDED_FOR" )
7
+ record .client_ip = (
8
+ x_forwarded_for .split ("," )[0 ]
9
+ if x_forwarded_for
10
+ else request .META .get ("REMOTE_ADDR" )
11
+ )
12
+ else :
13
+ record .client_ip = "unknown"
14
+ return True
You can’t perform that action at this time.
0 commit comments