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

Include exception stack trace in 'message' #131

Merged
merged 1 commit into from Apr 11, 2018

Conversation

johnpaulett
Copy link
Contributor

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 call logging.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 the exc_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:

  • As mentioned in the README, the formatter could be configured with '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.
  • There have been prior PRs (Add formatted exception #19 and Add exception traceback handling to Formatter #38) which have manually used the traceback module to simulate what LogRecord.exc_text provides.

@coveralls
Copy link

coveralls commented Apr 9, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling 39e60c0 on johnpaulett:include-stack-trace into 268f577 on fluent:master.

Brings fluent logging more inline with normal log formatters.

Utilize logging.Formatter.format() which combines record.msg
with the exc_text.

Signed-off-by: John Paulett <john@paulett.org>
@arcivanov arcivanov merged commit c107b95 into fluent:master Apr 11, 2018
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

3 participants