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

7927 unhandled error tb 3 #1039

Closed

Conversation

jlitzingerdev
Copy link
Contributor

@jlitzingerdev jlitzingerdev commented Jul 12, 2018

This introduces a new API to address issues where tracebacks for unhandled errors are not printed. There has been significant discussion in both the ticket and PRs, so for brevity I'm including links below.

The relevant tickets are:

This is the result of discussion on a review of an earlier candidate for 7927, located here: #1022 (comment)

eventAsText facilitates formatting a log event as unicode and
optionally including traceback, system, and timestamp information.
This API should be preferred over the existing formatEvent and
formatEventAsClassicLogText APIs.

One of the problems with the existing APIs is they do not always
include tracebacks, or handle multibyte/non-ascii encodings in the
tracebacks properly.  This API does both.

Update the narrative documentation to recommend this API over the
existing formatEvent API
The eventAsText API provides all necessary mechanisms to replace the
internals of the existing format APIs without changing the semantics
or form of the output.  This compatibility eliminates the need for the
existing implementations.
Currently, the default logger does not print tracebacks for unhandled
errors.  Using the new eventAsText API will ensure tracebacks are printed.
@jlitzingerdev
Copy link
Contributor Author

I'm almost certain the flagged txchecker issue:
329: W9401: (Used for checking comment format issues.), : Comments should begin with one whitespace is a bug in twistedchecker. It is interpreting the # in the system tag as a comment.

@jlitzingerdev
Copy link
Contributor Author

@glyph FYI, Initial PR for eventAsText.

@twm
Copy link
Contributor

twm commented Jul 17, 2018

It's definitely a txchecker bug: the same bit me on #1038. twisted/twistedchecker#110

Copy link
Member

@glyph glyph left a comment

Choose a reason for hiding this comment

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

I like pretty much everything about this except the dang newline. It just sticks in my craw :). Hopefully my explanation for why makes sense. If you feel strongly otherwise please feel free to respond with your reasoning about why you think it should be included (but you probably also want to fix the docs and stuff in that case to not double up on newlines).

Regarding the txchecker failures:

  • there are a number of actual lint errors, if you could fix those it would be great
  • you can work around the bug by spelling it '\x23' in tests. I realize this is a bit gross, but the standard thing to do is to work around issues like this and include a link to the txchecker bug so we can clean them up later, rather than littering the codebase with even more warnings that need to be ignored.

I think if you can address this stuff it's basically ready to land. When you submit for re-review please feel free to be annoying on the mailing list and such so we can do so quickly :).

@return: A formatted string with specified options.
@rtype: L{unicode}

@since: Twisted NEXT
Copy link
Member

Choose a reason for hiding this comment

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

🎉


logText += u"{eventText}".format(eventText=eventText)

return logText + u"\n"
Copy link
Member

Choose a reason for hiding this comment

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

The tacking on of the newline strikes me as a bit odd. If one is using eventAsText directly, one might wish to separate log messages by something other than newlines. (For example, they might be displayed in some kind of UI widget where the additional newline would space them out too much.) Why include it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is actually more an evolutionary artifact of how I developed it. I'm trying, but can't recall the rationale that led me to leave it in now. I'll take it out and fix the appropriate places.

if not eventText:
return eventText

logText = u""
Copy link
Member

Choose a reason for hiding this comment

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

Normally I wouldn't be so concerned with a tiny micro-optimization, but given that this has historically been a very hot code path in Twisted, I think it might be worthwhile to take some trouble to avoid having to rebuild the format string in its entirety on every single call.

(Let's not block on this; it can easily be done in a follow-up PR.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll spend a bit on it, but I won't let it add much delay.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@glyph Since you mentioned this is a hot path, it leads me to another question. There are helper functions _formatEvent, _formatTraceback used because (to me) they kept the code more readable. However, the function call has overhead. Should those be removed in favor of performance?


@provider(ILogObserver)
def simpleObserver(event):
print(formatEvent(event))
print(eventAsText(event))
Copy link
Member

Choose a reason for hiding this comment

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

This example now produces double the newlines.

includeTraceback=False,
includeTimestamp=False,
includeSystem=False,
)[:-1]
Copy link
Member

Choose a reason for hiding this comment

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

This :-1 seems like a rather unfortunate consequence of the newline being included. I realize that this is a "legacy" API now, ideally destined to be deprecated... but still, giving it intimate knowledge of the internals of another function seems weird. (Why :-1? What about \r\n newlines? What about record separators? etc.)

@jlitzingerdev
Copy link
Contributor Author

I'll fix up the style (sorry, that's an annoying miss on my part) and newline issues and resubmit, shouldn't be longer than the upcoming weekend. I'll also try to get the optimization done.

@codecov
Copy link

codecov bot commented Jul 21, 2018

Codecov Report

Merging #1039 into trunk will decrease coverage by 0.28%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##            trunk    #1039      +/-   ##
==========================================
- Coverage   91.85%   91.57%   -0.29%     
==========================================
  Files         844      844              
  Lines      150678   150807     +129     
  Branches    13147    13154       +7     
==========================================
- Hits       138402   138096     -306     
- Misses      10180    10598     +418     
- Partials     2096     2113      +17

jlitzingerdev and others added 7 commits July 20, 2018 22:13
Methods should have two blank lines between them, not one.
This works around a twistedchecker issue:

twisted/twistedchecker#110

Recommended during review.
This was raised during review.  There's no value in eventAsText
appending a newline to the resultant text.  The original goal was to
preserve the ending of the existing functions whose internals have
been replaced, but it isn't necessary for it to exist within
eventAsText to achieve that goal.

This change moves the newline out of eventAsText and back into the
appropriate functions (e.g. formatEventAsClassicLogText.
Rather than forcing the format string to be rebuilt on each call, have
it static and ensure that the appropriate whitespace/symbols are added
when the components (e.g. system) are set based on the include flags.

Raised during review as this has been hot path code in the past.
@hawkowl
Copy link
Member

hawkowl commented Aug 25, 2018

Merged! Thank you.

@hawkowl hawkowl closed this Aug 25, 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

4 participants