Skip to content

Commit abf957c

Browse files
authored
Merge pull request #11 from Colin-b/develop
Release version 0.3.0
2 parents 9b8e05c + c98280e commit abf957c

6 files changed

Lines changed: 53 additions & 8 deletions

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
repos:
22
- repo: https://github.com/psf/black
3-
rev: 21.12b0
3+
rev: 22.10.0
44
hooks:
55
- id: black

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.3.0] - 2022-12-02
10+
### Added
11+
- Added `exception_field_name` parameter.
12+
913
## [0.2.1] - 2022-01-26
1014
### Fixed
1115
- `datetime`, `time` and `date` instances are now represented following [ISO-8601](https://www.iso.org/iso-8601-date-and-time-format.html) format instead of raising a `TypeError`.
@@ -23,7 +27,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2327
### Added
2428
- Public release.
2529

26-
[Unreleased]: https://github.com/Colin-b/logging_json/compare/v0.2.1...HEAD
30+
[Unreleased]: https://github.com/Colin-b/logging_json/compare/v0.3.0...HEAD
31+
[0.3.0]: https://github.com/Colin-b/logging_json/compare/v0.2.1...v0.3.0
2732
[0.2.1]: https://github.com/Colin-b/logging_json/compare/v0.2.0...v0.2.1
2833
[0.2.0]: https://github.com/Colin-b/logging_json/compare/v0.1.0...v0.2.0
2934
[0.1.0]: https://github.com/Colin-b/logging_json/compare/v0.0.1...v0.1.0

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ This dictionary will contain 3 keys:
4040
* `message`: The str representation of the exception (usually the provided error message).
4141
* `stack`: The stack trace, formatted as a string.
4242

43+
You can rename the exception field key by setting the `exception_field_name` parameter with a new name for the key.
44+
It is also possible to disable this behaviour by setting the `exception_field_name` parameter to `None` or an empty string
45+
4346
### Logging with a dictionary
4447

4548
This formatter allows you to log dictionary as in the following:
@@ -57,7 +60,7 @@ The resulting JSON dictionary will be the one you provided (with the [additional
5760
Anything not logged using a dictionary will be handled by the standard formatter and it can result in one of the 2 output:
5861
* A JSON dictionary, if [additional fields](#adding-additional-fields-and-values) are set or if `extra` parameter is used while logging, with the message available in the `msg` key of the resulting JSON dictionary.
5962
Default `msg` key name can be changed by `message_field_name` parameter of the `logging_json.JSONFormatter` instance.
60-
* The formatted record, if no [additional fields](#adding-additional-fields-and-values) are set.
63+
* The formatted record, if no [additional fields](#adding-additional-fields-and-values) are set.
6164

6265
This handles the usual string logging as in the following:
6366

logging_json/_formatter.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import datetime
33
import json
44
import logging
5-
from typing import Any, Dict
5+
from typing import Any, Dict, Optional
66

77
standard_attributes = (
88
"name",
@@ -61,13 +61,15 @@ def __init__(
6161
*args,
6262
fields: Dict[str, Any] = None,
6363
message_field_name: str = "msg",
64+
exception_field_name: Optional[str] = "exception",
6465
**kwargs,
6566
):
6667
# Allow to provide any formatter setting (useful to provide a custom date format)
6768
super().__init__(*args, **kwargs)
6869
self.fields = fields or {}
6970
self.usesTime = lambda: "asctime" in self.fields.values()
7071
self.message_field_name = message_field_name
72+
self.exception_field_name = exception_field_name
7173

7274
def format(self, record: logging.LogRecord):
7375
# Let python set every additional record field
@@ -84,8 +86,8 @@ def format(self, record: logging.LogRecord):
8486

8587
message.update(_extra_attributes(record))
8688

87-
if record.exc_info:
88-
message["exception"] = {
89+
if self.exception_field_name and record.exc_info:
90+
message[self.exception_field_name] = {
8991
"type": record.exc_info[0].__name__,
9092
"message": str(record.exc_info[1]),
9193
"stack": self.formatException(record.exc_info),

logging_json/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
# Major should be incremented in case there is a breaking change. (eg: 2.5.8 -> 3.0.0)
44
# Minor should be incremented in case there is an enhancement. (eg: 2.5.8 -> 2.6.0)
55
# Patch should be incremented in case there is a bug fix. (eg: 2.5.8 -> 2.5.9)
6-
__version__ = "0.2.1"
6+
__version__ = "0.3.0"

tests/test_formatter.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import json
33
import time
44
import datetime
5+
import pytest
6+
import sys
57

68
import logging_json
79

@@ -114,6 +116,38 @@ def test_dict_message_at_exception_level(caplog):
114116
}
115117

116118

119+
def test_dict_message_at_exception_level_with_different_field_name(caplog):
120+
caplog.set_level("INFO")
121+
try:
122+
raise MyException("this is the exception message")
123+
except MyException:
124+
logging.exception({"key 1": "value 1", "key 2": 2})
125+
actual = dict_fmt(caplog, exception_field_name="info_about_exception")
126+
actual["info_about_exception"].pop("stack")
127+
assert actual == {
128+
"key 1": "value 1",
129+
"key 2": 2,
130+
"info_about_exception": {
131+
"message": "this is the exception message",
132+
"type": "MyException",
133+
},
134+
}
135+
136+
137+
@pytest.mark.parametrize("value", [None, ""])
138+
def test_dict_message_at_exception_level_without_exception_field(caplog, value):
139+
caplog.set_level("INFO")
140+
try:
141+
raise MyException("this is the exception message")
142+
except MyException:
143+
logging.exception({"key 1": "value 1", "key 2": 2})
144+
actual = dict_fmt(caplog, exception_field_name=value)
145+
assert actual == {
146+
"key 1": "value 1",
147+
"key 2": 2,
148+
}
149+
150+
117151
def test_str_message_at_exception_level(caplog):
118152
caplog.set_level("INFO")
119153
try:
@@ -179,6 +213,7 @@ def test_documented_record_attributes(caplog, monkeypatch):
179213
actual.pop("process_id")
180214
actual.pop("relative_timestamp")
181215
actual.pop("line_number")
216+
python310 = sys.version_info.minor >= 10
182217
assert actual == {
183218
"extra": "this is a value",
184219
"file_name": "test_formatter.py",
@@ -191,7 +226,7 @@ def test_documented_record_attributes(caplog, monkeypatch):
191226
"record_message": "{}",
192227
"thread_name": "MainThread",
193228
"timestamp": 1599736353.0076675,
194-
"timestamp_milliseconds": 7.66754150390625,
229+
"timestamp_milliseconds": 7.0 if python310 else 7.66754150390625,
195230
}
196231

197232

0 commit comments

Comments
 (0)