|
1 | 1 | import logging |
2 | 2 | import json |
3 | 3 | import time |
| 4 | +import datetime |
4 | 5 |
|
5 | 6 | import logging_json |
6 | 7 |
|
@@ -197,21 +198,43 @@ def test_documented_record_attributes(caplog, monkeypatch): |
197 | 198 | def test_with_extra_in_fields_and_message(caplog): |
198 | 199 | caplog.set_level("INFO") |
199 | 200 | 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(), |
213 | 229 | } |
214 | 230 | ) |
| 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 | + } |
215 | 238 |
|
216 | 239 |
|
217 | 240 | def fmt(caplog, *formatter_args, **formatter_kwargs) -> str: |
|
0 commit comments