Skip to content

Commit 150cc3e

Browse files
committed
Make some existing features extensible via constructor kwargs
1 parent 63873c1 commit 150cc3e

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/pythonjsonlogger/jsonlogger.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424
'msecs', 'message', 'msg', 'name', 'pathname', 'process',
2525
'processName', 'relativeCreated', 'stack_info', 'thread', 'threadName')
2626

27-
RESERVED_ATTR_HASH = dict(zip(RESERVED_ATTRS, RESERVED_ATTRS))
2827

29-
30-
def merge_record_extra(record, target, reserved=RESERVED_ATTR_HASH):
28+
def merge_record_extra(record, target, reserved):
3129
"""
3230
Merges extra attributes from LogRecord object into target dictionary
3331
@@ -80,14 +78,27 @@ def __init__(self, *args, **kwargs):
8078
:param json_encoder: optional custom encoder
8179
:param json_serializer: a :meth:`json.dumps`-compatible callable
8280
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.
8383
:param prefix: an optional string prefix added at the beginning of
8484
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.
8592
"""
8693
self.json_default = kwargs.pop("json_default", None)
8794
self.json_encoder = kwargs.pop("json_encoder", None)
8895
self.json_serializer = kwargs.pop("json_serializer", json.dumps)
8996
self.json_indent = kwargs.pop("json_indent", None)
9097
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+
91102
#super(JsonFormatter, self).__init__(*args, **kwargs)
92103
logging.Formatter.__init__(self, *args, **kwargs)
93104
if not self.json_encoder and not self.json_default:
@@ -96,7 +107,7 @@ def __init__(self, *args, **kwargs):
96107
self._required_fields = self.parse()
97108
self._skip_fields = dict(zip(self._required_fields,
98109
self._required_fields))
99-
self._skip_fields.update(RESERVED_ATTR_HASH)
110+
self._skip_fields.update(self.reserved_attrs)
100111

101112
def parse(self):
102113
"""
@@ -117,6 +128,10 @@ def add_fields(self, log_record, record, message_dict):
117128
log_record.update(message_dict)
118129
merge_record_extra(record, log_record, reserved=self._skip_fields)
119130

131+
if self.timestamp:
132+
key = self.timestamp if type(self.timestamp) == str else 'timestamp'
133+
log_record[key] = datetime.utcnow()
134+
120135
def process_log_record(self, log_record):
121136
"""
122137
Override this method to implement custom logic

0 commit comments

Comments
 (0)