24
24
'msecs' , 'message' , 'msg' , 'name' , 'pathname' , 'process' ,
25
25
'processName' , 'relativeCreated' , 'stack_info' , 'thread' , 'threadName' )
26
26
27
- RESERVED_ATTR_HASH = dict (zip (RESERVED_ATTRS , RESERVED_ATTRS ))
28
27
29
-
30
- def merge_record_extra (record , target , reserved = RESERVED_ATTR_HASH ):
28
+ def merge_record_extra (record , target , reserved ):
31
29
"""
32
30
Merges extra attributes from LogRecord object into target dictionary
33
31
@@ -80,14 +78,27 @@ def __init__(self, *args, **kwargs):
80
78
:param json_encoder: optional custom encoder
81
79
:param json_serializer: a :meth:`json.dumps`-compatible callable
82
80
that will be used to serialize the log record.
81
+ :param json_indent: an optional :meth:`json.dumps`-compatible numeric value
82
+ that will be used to customize the indent of the output json.
83
83
:param prefix: an optional string prefix added at the beginning of
84
84
the formatted string
85
+ :param reserved_attrs: an optional list of fields that will be skipped when
86
+ outputting json log record. Defaults to all log record attributes:
87
+ http://docs.python.org/library/logging.html#logrecord-attributes
88
+ :param timestamp: an optional string/boolean field to add a timestamp when
89
+ outputting the json log record. If string is passed, timestamp will be added
90
+ to log record using string as key. If True boolean is passed, timestamp key
91
+ will be "timestamp". Defaults to False/off.
85
92
"""
86
93
self .json_default = kwargs .pop ("json_default" , None )
87
94
self .json_encoder = kwargs .pop ("json_encoder" , None )
88
95
self .json_serializer = kwargs .pop ("json_serializer" , json .dumps )
89
96
self .json_indent = kwargs .pop ("json_indent" , None )
90
97
self .prefix = kwargs .pop ("prefix" , "" )
98
+ reserved_attrs = kwargs .pop ("reserved_attrs" , RESERVED_ATTRS )
99
+ self .reserved_attrs = dict (zip (reserved_attrs , reserved_attrs ))
100
+ self .timestamp = kwargs .pop ("timestamp" , False )
101
+
91
102
#super(JsonFormatter, self).__init__(*args, **kwargs)
92
103
logging .Formatter .__init__ (self , * args , ** kwargs )
93
104
if not self .json_encoder and not self .json_default :
@@ -96,7 +107,7 @@ def __init__(self, *args, **kwargs):
96
107
self ._required_fields = self .parse ()
97
108
self ._skip_fields = dict (zip (self ._required_fields ,
98
109
self ._required_fields ))
99
- self ._skip_fields .update (RESERVED_ATTR_HASH )
110
+ self ._skip_fields .update (self . reserved_attrs )
100
111
101
112
def parse (self ):
102
113
"""
@@ -117,6 +128,10 @@ def add_fields(self, log_record, record, message_dict):
117
128
log_record .update (message_dict )
118
129
merge_record_extra (record , log_record , reserved = self ._skip_fields )
119
130
131
+ if self .timestamp :
132
+ key = self .timestamp if type (self .timestamp ) == str else 'timestamp'
133
+ log_record [key ] = datetime .utcnow ()
134
+
120
135
def process_log_record (self , log_record ):
121
136
"""
122
137
Override this method to implement custom logic
0 commit comments