@@ -29,9 +29,14 @@ class FluentRecordFormatter(logging.Formatter, object):
29
29
key is not found. Put None if not found.
30
30
:param format_json: if True, will attempt to parse message as json. If not,
31
31
will use message as-is. Defaults to True
32
+ :param exclude_attrs: switches this formatter into a mode where all attributes
33
+ except the ones specified by `exclude_attrs` are logged with the record as is.
34
+ If `None`, operates as before, otherwise `fmt` is ignored.
35
+ Can be a `list`, `tuple` or a `set`.
32
36
"""
33
37
34
- def __init__ (self , fmt = None , datefmt = None , style = '%' , fill_missing_fmt_key = False , format_json = True ):
38
+ def __init__ (self , fmt = None , datefmt = None , style = '%' , fill_missing_fmt_key = False , format_json = True ,
39
+ exclude_attrs = None ):
35
40
super (FluentRecordFormatter , self ).__init__ (None , datefmt )
36
41
37
42
if sys .version_info [0 :2 ] >= (3 , 2 ) and style != '%' :
@@ -55,10 +60,15 @@ def __init__(self, fmt=None, datefmt=None, style='%', fill_missing_fmt_key=False
55
60
'sys_module' : '%(module)s' ,
56
61
}
57
62
58
- if not fmt :
59
- self ._fmt_dict = basic_fmt_dict
63
+ if exclude_attrs is not None :
64
+ self ._exc_attrs = set (exclude_attrs )
65
+ self ._fmt_dict = None
60
66
else :
61
- self ._fmt_dict = fmt
67
+ self ._exc_attrs = None
68
+ if not fmt :
69
+ self ._fmt_dict = basic_fmt_dict
70
+ else :
71
+ self ._fmt_dict = fmt
62
72
63
73
if format_json :
64
74
self ._format_msg = self ._format_msg_json
@@ -81,25 +91,33 @@ def format(self, record):
81
91
82
92
# Apply format
83
93
data = {}
84
- for key , value in self ._fmt_dict .items ():
85
- try :
86
- if self .__style :
87
- value = self .__style (value ).format (record )
88
- else :
89
- value = value % record .__dict__
90
- except KeyError as exc :
91
- value = None
92
- if not self .fill_missing_fmt_key :
93
- raise exc
94
-
95
- data [key ] = value
94
+ if self ._exc_attrs is not None :
95
+ for key , value in record .__dict__ .items ():
96
+ if key not in self ._exc_attrs :
97
+ data [key ] = value
98
+ else :
99
+ for key , value in self ._fmt_dict .items ():
100
+ try :
101
+ if self .__style :
102
+ value = self .__style (value ).format (record )
103
+ else :
104
+ value = value % record .__dict__
105
+ except KeyError as exc :
106
+ value = None
107
+ if not self .fill_missing_fmt_key :
108
+ raise exc
109
+
110
+ data [key ] = value
96
111
97
112
self ._structuring (data , record )
98
113
return data
99
114
100
115
def usesTime (self ):
101
- return any ([value .find ('%(asctime)' ) >= 0
102
- for value in self ._fmt_dict .values ()])
116
+ if self ._exc_attrs is not None :
117
+ return super (FluentRecordFormatter , self ).usesTime ()
118
+ else :
119
+ return any ([value .find ('%(asctime)' ) >= 0
120
+ for value in self ._fmt_dict .values ()])
103
121
104
122
def _structuring (self , data , record ):
105
123
""" Melds `msg` into `data`.
0 commit comments