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

Fix message formatting while handling exceptions #76

Merged

Conversation

bence-the-great
Copy link
Contributor

Consider the following format:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'logzioFormat': {
            'format': '{"additional_field": "value"}',
            'validate': False
        }
    },
    'handlers': {
        'logzio': {
            'class': 'logzio.handler.LogzioHandler',
            'level': 'INFO',
            'formatter': 'logzioFormat',
            'token': '<<LOGZIO-TOKEN>>',
            'logzio_type': 'python-handler',
            'logs_drain_timeout': 5,
            'url': 'https://<<LOGZIO-URL>>:8071',
            'retries_no': 4,
            'retry_timeout': 2,
        }
    },
    'loggers': {
        '': {
            'level': 'DEBUG',
            'handlers': ['logzio'],
            'propagate': True
        }
    }
}

And the case, where the user logs an exception:

logger.exception("Something")

Python appends the exception traceback to the formatted message ({"additional_field": "value"} originally). Which would result in the logzio handler not being able to parse it as JSON, and thus not adding the additional fields to the log.

Consider the following format:

```
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'logzioFormat': {
            'format': '{"additional_field": "value"}',
            'validate': False
        }
    },
    'handlers': {
        'logzio': {
            'class': 'logzio.handler.LogzioHandler',
            'level': 'INFO',
            'formatter': 'logzioFormat',
            'token': '<<LOGZIO-TOKEN>>',
            'logzio_type': 'python-handler',
            'logs_drain_timeout': 5,
            'url': 'https://<<LOGZIO-URL>>:8071',
            'retries_no': 4,
            'retry_timeout': 2,
        }
    },
    'loggers': {
        '': {
            'level': 'DEBUG',
            'handlers': ['logzio'],
            'propagate': True
        }
    }
}
```

And the case, where the user logs an exception:

```
logger.exception("Something")
```

Python appends the exception traceback to the formatted message
(`{"additional_field": "value"}` originally). Which would result in the
logzio handler not being able to parse it as JSON, and thus not adding
the additional fields to the log.
@shbedev
Copy link

shbedev commented May 2, 2023

Encountered the same issue. Would be very helpful if this will be merged.

@mirii1994 mirii1994 self-requested a review May 3, 2023 10:56
@@ -79,6 +79,8 @@ def flush(self):
def format(self, record):
message = super(LogzioHandler, self).format(record)
try:
if record.exc_info:
message = message.split("\nTraceback")[0] # only keep the original formatted message part
Copy link
Contributor

@mirii1994 mirii1994 May 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reporting this issue.
When I tried recreating with the example you provided, the field message looks like this:
'{"additional_field": "value"}\nNoneType: None'
So this line doesn't fix it for me.
What about message = message.split("\n")[0] ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reporting this issue. When I tried recreating with the example you provided, the field message looks like this: '{"additional_field": "value"}\nNoneType: None' So this line doesn't fix it for me. What about message = message.split("\n")[0] ?

I agree with @mirii1994 's proposed change. Would it be possible to expedite this? Happy to take over if needed!

@mirii1994 mirii1994 merged commit 6ccf4c9 into logzio:master May 21, 2023
3 checks passed
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

Successfully merging this pull request may close these issues.

None yet

4 participants