From 6ccf4c9614679c774c64fb1ce2c3416965f4b863 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Szab=C3=B3?= Date: Sun, 21 May 2023 09:14:50 +0200 Subject: [PATCH] Fix message formatting while handling exceptions (#76) * Fix message formatting while handling exceptions * change split, add changelog * bump version --------- Co-authored-by: mirii1994 --- README.md | 11 ++++++++--- logzio/handler.py | 2 ++ setup.py | 2 +- tests/test_logzioHandler.py | 11 ++++++++--- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 189784e..a9b71fd 100644 --- a/README.md +++ b/README.md @@ -255,6 +255,9 @@ LOGGING = { Please note that if you are using `python 3.8`, it is preferred to use the `logging.config.dictConfig` method, as mentioned in [python's documentation](https://docs.python.org/3/library/logging.config.html#configuration-file-format). ## Release Notes + +- 4.0.2 + - Fix bug for logging exceptions ([#76](https://github.com/logzio/logzio-python-handler/pull/76)) - 4.0.1 - Updated `protobuf>=3.20.2`. - Added dependency `setuptools>=65.5.1` @@ -262,13 +265,15 @@ Please note that if you are using `python 3.8`, it is preferred to use the `logg - 4.0.0 - Add ability to automatically attach trace context to the logs. -- 3.1.1 - - Bug fixes (issue #68, exception message formatting) - - Added CI: Tests and Auto release
Expand to check old versions + +- 3.1.1 + - Bug fixes (issue #68, exception message formatting) + - Added CI: Tests and Auto release + - 3.1.0 - Bug fixes - Retry number and timeout is now configurable diff --git a/logzio/handler.py b/logzio/handler.py index e9f21e4..b81dde9 100644 --- a/logzio/handler.py +++ b/logzio/handler.py @@ -79,6 +79,8 @@ class LogzioHandler(logging.Handler): def format(self, record): message = super(LogzioHandler, self).format(record) try: + if record.exc_info: + message = message.split("\n")[0] # only keep the original formatted message part return json.loads(message) except (TypeError, ValueError): return message diff --git a/setup.py b/setup.py index d88aada..00c8b24 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools import setup, find_packages setup( name="logzio-python-handler", - version='4.0.1', + version='4.0.2', description="Logging handler to send logs to your Logz.io account with bulk SSL", keywords="logging handler logz.io bulk https", author="roiravhon", diff --git a/tests/test_logzioHandler.py b/tests/test_logzioHandler.py index e2ae4a1..c42ec89 100644 --- a/tests/test_logzioHandler.py +++ b/tests/test_logzioHandler.py @@ -137,7 +137,10 @@ class TestLogzioHandler(TestCase): } ) - def test_exc(self): + def test_exception(self): + formatter = logging.Formatter('{"tags": ["staging", "experimental"], "appname": "my-service"}', validate=False) + self.handler.setFormatter(formatter) + try: raise ValueError("oops.") except: @@ -163,13 +166,15 @@ class TestLogzioHandler(TestCase): self.assertDictEqual( { '@timestamp': None, + 'appname': 'my-service', 'line_number': 10, 'log_level': 'NOTSET', 'logger': 'my-logger', 'message': 'exception test:', - 'exception': 'Traceback (most recent call last):\n\n File "", in test_exc\n raise ValueError("oops.")\n\nValueError: oops.\n', + 'exception': 'Traceback (most recent call last):\n\n File "", in test_exception\n raise ValueError("oops.")\n\nValueError: oops.\n', 'path_name': 'handler_test.py', - 'type': 'python' + 'type': 'python', + 'tags': ['staging', 'experimental'] }, formatted_message )