Skip to content

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.
@bence-the-great bence-the-great force-pushed the fix-exception-message-formatting branch from 90287ab to 489f90b Compare February 22, 2023 07:02
@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
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
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.

4 participants