Skip to content

Commit 9b8e05c

Browse files
authored
Merge pull request #8 from Colin-b/develop
Release 0.2.1
2 parents a3e71e0 + 709871a commit 9b8e05c

7 files changed

Lines changed: 62 additions & 27 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.11b1
3+
rev: 21.12b0
44
hooks:
55
- id: black

CHANGELOG.md

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

77
## [Unreleased]
88

9+
## [0.2.1] - 2022-01-26
10+
### Fixed
11+
- `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`.
12+
- Default to the `str` representation of value instead of raising a `TypeError` for non-standard python types.
13+
914
## [0.2.0] - 2021-11-24
1015
### Added
1116
- Added `message_field_name` parameter.
@@ -18,7 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1823
### Added
1924
- Public release.
2025

21-
[Unreleased]: https://github.com/Colin-b/healthpy/compare/v0.2.0...HEAD
22-
[0.2.0]: https://github.com/Colin-b/healthpy/compare/v0.1.0...v0.2.0
23-
[0.1.0]: https://github.com/Colin-b/healthpy/compare/v0.0.1...v0.1.0
24-
[0.0.1]: https://github.com/Colin-b/healthpy/releases/tag/v0.0.1
26+
[Unreleased]: https://github.com/Colin-b/logging_json/compare/v0.2.1...HEAD
27+
[0.2.1]: https://github.com/Colin-b/logging_json/compare/v0.2.0...v0.2.1
28+
[0.2.0]: https://github.com/Colin-b/logging_json/compare/v0.1.0...v0.2.0
29+
[0.1.0]: https://github.com/Colin-b/logging_json/compare/v0.0.1...v0.1.0
30+
[0.0.1]: https://github.com/Colin-b/logging_json/releases/tag/v0.0.1

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Colin Bounouar
3+
Copyright (c) 2022 Colin Bounouar
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<a href="https://github.com/Colin-b/logging_json/actions"><img alt="Build status" src="https://github.com/Colin-b/logging_json/workflows/Release/badge.svg"></a>
66
<a href="https://github.com/Colin-b/logging_json/actions"><img alt="Coverage" src="https://img.shields.io/badge/coverage-100%25-brightgreen"></a>
77
<a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
8-
<a href="https://github.com/Colin-b/logging_json/actions"><img alt="Number of tests" src="https://img.shields.io/badge/tests-17 passed-blue"></a>
8+
<a href="https://github.com/Colin-b/logging_json/actions"><img alt="Number of tests" src="https://img.shields.io/badge/tests-18 passed-blue"></a>
99
<a href="https://pypi.org/project/logging_json/"><img alt="Number of downloads" src="https://img.shields.io/pypi/dm/logging_json"></a>
1010
</p>
1111

@@ -35,7 +35,7 @@ It must be a dictionary where keys are the keys to be appended to the resulting
3535

3636
If an exception is logged, the `exception` key will be appended to the resulting JSON dictionary.
3737

38-
This dictionary will contains 3 keys:
38+
This dictionary will contain 3 keys:
3939
* `type`: The name of the exception class (useful when the message is blank).
4040
* `message`: The str representation of the exception (usually the provided error message).
4141
* `stack`: The stack trace, formatted as a string.

logging_json/_formatter.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import collections
2+
import datetime
23
import json
34
import logging
45
from typing import Any, Dict
@@ -48,6 +49,12 @@ def _value(record: logging.LogRecord, field_name_or_value: Any) -> Any:
4849
return field_name_or_value
4950

5051

52+
def default_converter(obj: Any) -> str:
53+
if isinstance(obj, datetime.datetime):
54+
return obj.isoformat()
55+
return str(obj)
56+
57+
5158
class JSONFormatter(logging.Formatter):
5259
def __init__(
5360
self,
@@ -84,11 +91,10 @@ def format(self, record: logging.LogRecord):
8491
"stack": self.formatException(record.exc_info),
8592
}
8693

87-
return (
88-
super().formatMessage(record)
89-
if (len(message) == 1 and self.message_field_name in message)
90-
else json.dumps(message)
91-
)
94+
if len(message) == 1 and self.message_field_name in message:
95+
return super().formatMessage(record)
96+
97+
return json.dumps(message, default=default_converter)
9298

9399
def formatMessage(self, record: logging.LogRecord) -> str:
94100
# Speed up this step by doing nothing

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.0"
6+
__version__ = "0.2.1"

tests/test_formatter.py

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import json
33
import time
4+
import datetime
45

56
import logging_json
67

@@ -197,21 +198,43 @@ def test_documented_record_attributes(caplog, monkeypatch):
197198
def test_with_extra_in_fields_and_message(caplog):
198199
caplog.set_level("INFO")
199200
logging.info("message 1", extra={"key1": "value 1"})
200-
assert (
201-
dict_fmt(
202-
caplog,
203-
fields={
204-
"extra": "key1",
205-
"key2": "value 2",
206-
},
207-
)
208-
== {
209-
"extra": "value 1",
210-
"key1": "value 1",
211-
"key2": "value 2",
212-
"msg": "message 1",
201+
assert dict_fmt(caplog, fields={"extra": "key1", "key2": "value 2",},) == {
202+
"extra": "value 1",
203+
"key1": "value 1",
204+
"key2": "value 2",
205+
"msg": "message 1",
206+
}
207+
208+
209+
def test_json_dumps_error(caplog):
210+
class CustomWithoutStr:
211+
pass
212+
213+
custom = CustomWithoutStr()
214+
215+
class CustomWithStr:
216+
def __str__(self):
217+
return "Custom instance"
218+
219+
caplog.set_level("INFO")
220+
logging.info(
221+
{
222+
"my_datetime": datetime.datetime(
223+
2020, 1, 10, 3, 14, 11, tzinfo=datetime.timezone.utc
224+
),
225+
"my_date": datetime.date(2020, 1, 10),
226+
"my_time": datetime.time(3, 14, 11, tzinfo=datetime.timezone.utc),
227+
"my_custom_obj": custom,
228+
"my_custom_obj_with_str": CustomWithStr(),
213229
}
214230
)
231+
assert dict_fmt(caplog,) == {
232+
"my_custom_obj": str(custom),
233+
"my_custom_obj_with_str": "Custom instance",
234+
"my_date": "2020-01-10",
235+
"my_datetime": "2020-01-10T03:14:11+00:00",
236+
"my_time": "03:14:11+00:00",
237+
}
215238

216239

217240
def fmt(caplog, *formatter_args, **formatter_kwargs) -> str:

0 commit comments

Comments
 (0)