Skip to content

Commit e148ffc

Browse files
committed
feat(logging): allow for custom json order
Changes: * add `format_key` to allow for a custom order * remove unessarly verbose `json_formatter` with just `str` * update tests
1 parent a9cb740 commit e148ffc

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

aws_lambda_powertools/logging/formatter.py

+5-29
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
import json
22
import logging
3-
from typing import Any
4-
5-
6-
def json_formatter(unserializable_value: Any):
7-
"""JSON custom serializer to cast unserializable values to strings.
8-
9-
Example
10-
-------
11-
12-
**Serialize unserializable value to string**
13-
14-
class X: pass
15-
value = {"x": X()}
16-
17-
json.dumps(value, default=json_formatter)
18-
19-
Parameters
20-
----------
21-
unserializable_value: Any
22-
Python object unserializable by JSON
23-
"""
24-
return str(unserializable_value)
253

264

275
class JsonFormatter(logging.Formatter):
@@ -44,17 +22,15 @@ def __init__(self, **kwargs):
4422
4523
Other kwargs are used to specify log field format strings.
4624
"""
47-
self.default_json_formatter = kwargs.pop("json_default", json_formatter)
25+
self.default_json_formatter = kwargs.pop("json_default", str)
4826
datefmt = kwargs.pop("datefmt", None)
4927

5028
super(JsonFormatter, self).__init__(datefmt=datefmt)
5129
self.reserved_keys = ["timestamp", "level", "location"]
52-
self.format_dict = {
53-
"timestamp": "%(asctime)s",
54-
"level": "%(levelname)s",
55-
"location": "%(funcName)s:%(lineno)d",
56-
"message": None,
57-
}
30+
self.format_dict = dict.fromkeys(kwargs.pop("format_key", ["level", "location", "message", "timestamp"]))
31+
self.format_dict.update(
32+
{"level": "%(levelname)s", "location": "%(funcName)s:%(lineno)d", "timestamp": "%(asctime)s"}
33+
)
5834
self.format_dict.update(kwargs)
5935

6036
def update_formatter(self, **kwargs):

tests/functional/test_aws_lambda_logging.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,18 @@ def test_log_dict_key_seq(stdout):
127127

128128
log_dict: dict = json.loads(stdout.getvalue())
129129

130-
# THEN the key sequence should be `timestamp,level,location,message`
131-
assert ",".join(list(log_dict.keys())[:4]) == "timestamp,level,location,message"
130+
# THEN the key sequence should be `level,location,message,timestamp`
131+
assert ",".join(list(log_dict.keys())[:4]) == "level,location,message,timestamp"
132+
133+
134+
def test_log_dict_key_custom_seq(stdout):
135+
# GIVEN any logger configuration
136+
logger = Logger(level="INFO", stream=stdout, another="xxx", format_key=["message"])
137+
138+
# WHEN logging a message
139+
logger.info("Message")
140+
141+
log_dict: dict = json.loads(stdout.getvalue())
142+
143+
# THEN the key sequence should be `level,location,message,timestamp`
144+
assert ",".join(list(log_dict.keys())[:4]) == "message,level,location,timestamp"

0 commit comments

Comments
 (0)