Browse Source

Fix message formatting while handling exceptions (#76)

* Fix message formatting while handling exceptions

* change split, add changelog

* bump version

---------

Co-authored-by: mirii1994 <miri.ignatiev@logz.io>
master
Bence Szabó 2 years ago
committed by GitHub
parent
commit
6ccf4c9614
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 7 deletions
  1. +8
    -3
      README.md
  2. +2
    -0
      logzio/handler.py
  3. +1
    -1
      setup.py
  4. +8
    -3
      tests/test_logzioHandler.py

+ 8
- 3
README.md View File

@ -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). 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 ## Release Notes
- 4.0.2
- Fix bug for logging exceptions ([#76](https://github.com/logzio/logzio-python-handler/pull/76))
- 4.0.1 - 4.0.1
- Updated `protobuf>=3.20.2`. - Updated `protobuf>=3.20.2`.
- Added dependency `setuptools>=65.5.1` - 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 - 4.0.0
- Add ability to automatically attach trace context to the logs. - 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
<details> <details>
<summary markdown="span"> Expand to check old versions </summary> <summary markdown="span"> Expand to check old versions </summary>
- 3.1.1
- Bug fixes (issue #68, exception message formatting)
- Added CI: Tests and Auto release
- 3.1.0 - 3.1.0
- Bug fixes - Bug fixes
- Retry number and timeout is now configurable - Retry number and timeout is now configurable


+ 2
- 0
logzio/handler.py View File

@ -79,6 +79,8 @@ class LogzioHandler(logging.Handler):
def format(self, record): def format(self, record):
message = super(LogzioHandler, self).format(record) message = super(LogzioHandler, self).format(record)
try: try:
if record.exc_info:
message = message.split("\n")[0] # only keep the original formatted message part
return json.loads(message) return json.loads(message)
except (TypeError, ValueError): except (TypeError, ValueError):
return message return message


+ 1
- 1
setup.py View File

@ -3,7 +3,7 @@
from setuptools import setup, find_packages from setuptools import setup, find_packages
setup( setup(
name="logzio-python-handler", 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", description="Logging handler to send logs to your Logz.io account with bulk SSL",
keywords="logging handler logz.io bulk https", keywords="logging handler logz.io bulk https",
author="roiravhon", author="roiravhon",


+ 8
- 3
tests/test_logzioHandler.py View File

@ -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: try:
raise ValueError("oops.") raise ValueError("oops.")
except: except:
@ -163,13 +166,15 @@ class TestLogzioHandler(TestCase):
self.assertDictEqual( self.assertDictEqual(
{ {
'@timestamp': None, '@timestamp': None,
'appname': 'my-service',
'line_number': 10, 'line_number': 10,
'log_level': 'NOTSET', 'log_level': 'NOTSET',
'logger': 'my-logger', 'logger': 'my-logger',
'message': 'exception test:', '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', 'path_name': 'handler_test.py',
'type': 'python'
'type': 'python',
'tags': ['staging', 'experimental']
}, },
formatted_message formatted_message
) )

Loading…
Cancel
Save