nhairs / python-json-logger

JSON Formatter for Python Logging
https://nhairs.github.io/python-json-logger/
BSD 2-Clause "Simplified" License
28 stars 5 forks source link

Tests failing with Python 3.12 (taskName) #3

Closed nhairs closed 8 months ago

nhairs commented 8 months ago

Original Issue: https://github.com/madzak/python-json-logger/issues/185

With Python 3.12 we are seeing the following test cases fail. They don't fail with Python 3.11.

python-json-logger> _______________ TestJsonLogger.test_custom_object_serialization ________________
python-json-logger> 
python-json-logger> self = <tests.test_jsonlogger.TestJsonLogger testMethod=test_custom_object_serialization>
python-json-logger> 
python-json-logger>     def test_custom_object_serialization(self):
python-json-logger>         def encode_complex(z):
python-json-logger>             if isinstance(z, complex):
python-json-logger>                 return (z.real, z.imag)
python-json-logger>             else:
python-json-logger>                 type_name = z.__class__.__name__
python-json-logger>                 raise TypeError("Object of type '{}' is no JSON serializable".format(type_name))
python-json-logger>     
python-json-logger>         formatter = jsonlogger.JsonFormatter(json_default=encode_complex,
python-json-logger>                                              json_encoder=json.JSONEncoder)
python-json-logger>         self.log_handler.setFormatter(formatter)
python-json-logger>     
python-json-logger>         value = {
python-json-logger>             "special": complex(3, 8),
python-json-logger>         }
python-json-logger>     
python-json-logger>         self.log.info(" message", extra=value)
python-json-logger>         msg = self.buffer.getvalue()
python-json-logger> >       self.assertEqual(msg, "{\"message\": \" message\", \"special\": [3.0, 8.0]}\n")
python-json-logger> E       AssertionError: '{"message": " message", "taskName": null, "special": [3.0, 8.0]}\n' != '{"message": " message", "special": [3.0, 8.0]}\n'
python-json-logger> E       - {"message": " message", "taskName": null, "special": [3.0, 8.0]}
python-json-logger> E       ?                        ------------------
python-json-logger> E       + {"message": " message", "special": [3.0, 8.0]}
python-json-logger> 
python-json-logger> tests/test_jsonlogger.py:277: AssertionError
python-json-logger> ------------------------------ Captured log call -------------------------------
python-json-logger> INFO     logging-test-99:test_jsonlogger.py:275  message
python-json-logger> ____________________ TestJsonLogger.test_percentage_format _____________________
python-json-logger> 
python-json-logger> self = <tests.test_jsonlogger.TestJsonLogger testMethod=test_percentage_format>
python-json-logger> 
python-json-logger>     def test_percentage_format(self):
python-json-logger>         fr = jsonlogger.JsonFormatter(
python-json-logger>             # All kind of different styles to check the regex
python-json-logger>             '[%(levelname)8s] %(message)s %(filename)s:%(lineno)d %(asctime)'
python-json-logger>         )
python-json-logger>         self.log_handler.setFormatter(fr)
python-json-logger>     
python-json-logger>         msg = "testing logging format"
python-json-logger>         self.log.info(msg)
python-json-logger>         log_json = json.loads(self.buffer.getvalue())
python-json-logger>     
python-json-logger>         self.assertEqual(log_json["message"], msg)
python-json-logger> >       self.assertEqual(log_json.keys(), {'levelname', 'message', 'filename', 'lineno', 'asctime'})
python-json-logger> E       AssertionError: dict_keys(['levelname', 'message', 'filename', 'lineno', 'asctime', 'taskName']) != {'asctime', 'levelname', 'message', 'filename', 'lineno'}
python-json-logger> 
python-json-logger> tests/test_jsonlogger.py:53: AssertionError
python-json-logger> ------------------------------ Captured log call -------------------------------
python-json-logger> INFO     logging-test-51:test_jsonlogger.py:49 testing logging format
python-json-logger> __________________ TestJsonLogger.test_rename_reserved_attrs ___________________
python-json-logger> 
python-json-logger> self = <tests.test_jsonlogger.TestJsonLogger testMethod=test_rename_reserved_attrs>
python-json-logger> 
python-json-logger>     def test_rename_reserved_attrs(self):
python-json-logger>         log_format = lambda x: ['%({0:s})s'.format(i) for i in x]
python-json-logger>         reserved_attrs_map = {
python-json-logger>             'exc_info': 'error.type',
python-json-logger>             'exc_text': 'error.message',
python-json-logger>             'funcName': 'log.origin.function',
python-json-logger>             'levelname': 'log.level',
python-json-logger>             'module': 'log.origin.file.name',
python-json-logger>             'processName': 'process.name',
python-json-logger>             'threadName': 'process.thread.name',
python-json-logger>             'msg': 'log.message'
python-json-logger>         }
python-json-logger>     
python-json-logger>         custom_format = ' '.join(log_format(reserved_attrs_map.keys()))
python-json-logger>         reserved_attrs = [_ for _ in jsonlogger.RESERVED_ATTRS if _ not in list(reserved_attrs_map.keys())]
python-json-logger>         formatter = jsonlogger.JsonFormatter(custom_format, reserved_attrs=reserved_attrs, rename_fields=reserved_attrs_map)
python-json-logger>         self.log_handler.setFormatter(formatter)
python-json-logger>         self.log.info("message")
python-json-logger>     
python-json-logger>         msg = self.buffer.getvalue()
python-json-logger> >       self.assertEqual(msg, '{"error.type": null, "error.message": null, "log.origin.function": "test_rename_reserved_attrs", "log.level": "INFO", "log.origin.file.name": "test_jsonlogger", "process.name": "MainProcess", "process.thread.name": "MainThread", "log.message": "message"}\n')
python-json-logger> E       AssertionError: '{"taskName": null, "error.type": null, "err[227 chars]"}\n' != '{"error.type": null, "error.message": null,[209 chars]"}\n'
python-json-logger> E       - {"taskName": null, "error.type": null, "error.message": null, "log.origin.function": "test_rename_reserved_attrs", "log.level": "INFO", "log.origin.file.name": "test_jsonlogger", "process.name": "MainProcess", "process.thread.name": "MainThread", "log.message": "message"}
python-json-logger> E       ?  ------------------
python-json-logger> E       + {"error.type": null, "error.message": null, "log.origin.function": "test_rename_reserved_attrs", "log.level": "INFO", "log.origin.file.name": "test_jsonlogger", "process.name": "MainProcess", "process.thread.name": "MainThread", "log.message": "message"}
python-json-logger> 
python-json-logger> tests/test_jsonlogger.py:299: AssertionError
python-json-logger> ------------------------------ Captured log call -------------------------------
python-json-logger> INFO     logging-test-68:test_jsonlogger.py:296 message

Per this comment

gh-91513: Added taskName attribute to logging module for use with asyncio tasks. python/cpython#91513