Include exception stack trace in 'message' #131
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem: Since switching from the builtin log handlers to fluent-logger-python, when an exception occurs in my web application, I only receive the single line from
LogRecord.msg
(e.g.{"message": "Internal Server Error: /"
). The traceback and root exception are not present, making debugging nearly impossible.Solution: In this PR, when we do not have a dict or JSON message, instead of using
LogRecord.msg
we calllogging.Formatter.format()
. For existing debug/info/warn/error string messages, this will result in no change, but when there is an exception, it will include theexc_text
in the message, as is common with the builtin logging Formatters. For example:{'message': 'it failed\nTraceback (most recent call last):\n File "[..]/fluent-logger-python/tests/test_handler.py", line 363, in test_exception_message\n raise Exception(\'sample exception\')\nException: sample exception'}
Alternative Solutions:
'stack_trace': '%(exc_text)s'
. The downside to this approach is that 1) the majority of log statements which lack an exception will have an unnecessary"stack_trace": "None"
, 2) it is surprising behavior to new library users that the message does not include the normal trace info.