Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests failing with Python 3.12 #185

Open
mweinelt opened this issue Dec 6, 2023 · 3 comments
Open

Tests failing with Python 3.12 #185

mweinelt opened this issue Dec 6, 2023 · 3 comments

Comments

@mweinelt
Copy link

mweinelt commented Dec 6, 2023

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
@finswimmer
Copy link

I guess this could be fixed by #183

Unfortunately no one had the time to review it until now :(

@mweinelt
Copy link
Author

It does, thank you.

@nhairs
Copy link

nhairs commented Mar 24, 2024

I have forked this project and fixed this issue see: nhairs/python-json-logger#3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants