@@ -304,46 +304,66 @@ 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
+ args [key ] = str (self .anonip .process_ip (ip ))
336
+ if not has_setitem :
337
+ args = orig_type (args )
338
+ return args
339
+
320
340
def filter (self , record ):
321
341
"""
322
- See logging.Filter.filter()
342
+ Apply anonip IP masking.
343
+
344
+ :param record: logging.LogRecord
345
+ :return: bool
323
346
"""
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 ))
347
+ if not super (AnonipFilter , self ).filter (record ):
348
+ return False
349
+
350
+ for key in self .args :
351
+ if isinstance (record .args , abc .Mapping ):
352
+ if key in record .args :
353
+ record .args = self ._set_args_attr (record .args , key )
354
+ elif isinstance (record .args , abc .Sequence ):
355
+ if isinstance (key , int ) and key < len (record .args ):
356
+ record .args = self ._set_args_attr (record .args , key )
357
+
358
+ for key in self .extra :
359
+ if hasattr (record , key ):
360
+ value = getattr (record , key )
361
+ if isinstance (value , str ):
362
+ ip = self .anonip .extract_ip (value )[1 ]
363
+ setattr (record , key , str (self .anonip .process_ip (ip )))
364
+
365
+ if not self .args and not self .extra :
366
+ record .msg = self .anonip .process_line (record .msg )
347
367
348
368
return True
349
369
0 commit comments