@@ -304,46 +304,68 @@ def truncate_address(self, ip):
304
304
return ip .supernet (new_prefix = self ._prefixes [ip .version ])[0 ]
305
305
306
306
307
- class AnonipFilter :
308
- def __init__ (self , args = None , extra = None , anonip = None ):
307
+ class AnonipFilter ( logging . Filter ) :
308
+ def __init__ (self , name = "" , args = None , extra = None , anonip = None ):
309
309
"""
310
310
An implementation of Python logging.Filter using anonip.
311
311
312
+ :param name: str
312
313
:param args: list of log message args to filter. Defaults to []
313
314
:param extra: list of LogRecord attributes to filter. Defaults to []
314
315
:param anonip: dict of parameters for Anonip instance
315
316
"""
317
+ super (AnonipFilter , self ).__init__ (name )
316
318
self .args = [] if args is None else args
317
319
self .extra = [] if extra is None else extra
318
320
self .anonip = Anonip (** (anonip or {}))
319
321
322
+ def _set_args_attr (self , args , key ):
323
+ value = args [key ]
324
+ if not isinstance (value , str ):
325
+ return args
326
+
327
+ orig_type = type (args )
328
+ temp_type = list
329
+ if isinstance (args , abc .Mapping ):
330
+ temp_type = dict
331
+ has_setitem = hasattr (args , "__setitem__" )
332
+ if not has_setitem :
333
+ args = temp_type (args )
334
+ ip = self .anonip .extract_ip (value )[1 ]
335
+ if ip :
336
+ args [key ] = str (self .anonip .process_ip (ip ))
337
+ if not has_setitem :
338
+ args = orig_type (args )
339
+ return args
340
+
320
341
def filter (self , record ):
321
342
"""
322
- See logging.Filter.filter()
343
+ Apply anonip IP masking.
344
+
345
+ :param record: logging.LogRecord
346
+ :return: bool
323
347
"""
324
- if record .name != "anonip" :
325
- for key in self .args :
326
- if isinstance (record .args , abc .Mapping ):
327
- if key in record .args :
328
- value = record .args [key ]
329
- if isinstance (value , str ):
330
- record .args [key ] = self .anonip .process_line (value )
331
- elif isinstance (record .args , abc .Sequence ):
332
- if key < len (record .args ):
333
- value = record .args [key ]
334
- if isinstance (value , str ):
335
- is_tuple = isinstance (record .args , tuple )
336
- if is_tuple :
337
- record .args = list (record .args )
338
- record .args [key ] = self .anonip .process_line (value )
339
- if is_tuple :
340
- record .args = tuple (record .args )
341
-
342
- for key in self .extra :
343
- if hasattr (record , key ):
344
- value = getattr (record , key )
345
- if (isinstance (value , str )):
346
- setattr (record , key , self .anonip .process_line (value ))
348
+ if not super (AnonipFilter , self ).filter (record ):
349
+ return False
350
+
351
+ for key in self .args :
352
+ if isinstance (record .args , abc .Mapping ):
353
+ if key in record .args :
354
+ record .args = self ._set_args_attr (record .args , key )
355
+ elif isinstance (record .args , abc .Sequence ):
356
+ if isinstance (key , int ) and key < len (record .args ):
357
+ record .args = self ._set_args_attr (record .args , key )
358
+
359
+ for key in self .extra :
360
+ if hasattr (record , key ):
361
+ value = getattr (record , key )
362
+ if isinstance (value , str ):
363
+ ip = self .anonip .extract_ip (value )[1 ]
364
+ if ip :
365
+ setattr (record , key , str (self .anonip .process_ip (ip )))
366
+
367
+ # IP is not in args or extra, but in msg
368
+ record .msg = self .anonip .process_line (record .msg )
347
369
348
370
return True
349
371
0 commit comments