From 14eb7c3a4f74a7fc3bbede2450d42f0197d3bac3 Mon Sep 17 00:00:00 2001 From: Neil Williams Date: Wed, 1 May 2013 16:12:51 -0700 Subject: [PATCH] logging: Improve the quality of tracebacks from ErrorController. --- r2/r2/controllers/error.py | 23 ++++++++++++----------- r2/r2/lib/log.py | 15 ++++++++------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/r2/r2/controllers/error.py b/r2/r2/controllers/error.py index 61327b7de..668727f1c 100644 --- a/r2/r2/controllers/error.py +++ b/r2/r2/controllers/error.py @@ -38,7 +38,7 @@ try: from r2.controllers.reddit_base import RedditController, Cookies from r2.lib.errors import ErrorSet from r2.lib.filters import websafe_json - from r2.lib import pages + from r2.lib import log, pages from r2.lib.strings import rand_strings from r2.lib.template_helpers import static from r2.models.link import Link @@ -101,20 +101,20 @@ class ErrorController(RedditController): except (HTTPMovedPermanently, HTTPFound): # ignore an attempt to redirect from an error page pass - except: - handle_awful_failure("Error occurred in ErrorController.__before__") + except Exception as e: + handle_awful_failure("ErrorController.__before__: %r" % e) def __after__(self): try: RedditController.__after__(self) - except: - handle_awful_failure("Error occurred in ErrorController.__after__") + except Exception as e: + handle_awful_failure("ErrorController.__after__: %r" % e) def __call__(self, environ, start_response): try: return RedditController.__call__(self, environ, start_response) - except: - return handle_awful_failure("something really awful just happened.") + except Exception as e: + return handle_awful_failure("ErrorController.__call__: %r" % e) def send403(self): @@ -202,8 +202,8 @@ class ErrorController(RedditController): return self.send404() else: return "page not found" - except: - return handle_awful_failure("something really bad just happened.") + except Exception as e: + return handle_awful_failure("ErrorController.GET_document: %r" % e) POST_document = GET_document @@ -220,8 +220,9 @@ def handle_awful_failure(fail_text): try: # log the traceback, and flag the "path" as the error location import traceback - g.log.error("FULLPATH: %s" % fail_text) - g.log.error(traceback.format_exc()) + log.write_error_summary(fail_text) + for line in traceback.format_exc().splitlines(): + g.log.error(line) return redditbroke % (make_failien_url(), fail_text) except: # we are doomed. Admit defeat diff --git a/r2/r2/lib/log.py b/r2/r2/lib/log.py index e8ec072a9..04b8d5b4d 100644 --- a/r2/r2/lib/log.py +++ b/r2/r2/lib/log.py @@ -118,6 +118,13 @@ class LogQueueErrorReporter(Reporter): amqp.add_item(QUEUE_NAME, cPickle.dumps(d)) +def write_error_summary(error): + """Log a single-line summary of the error for easy log grepping.""" + fullpath = request.environ.get('FULLPATH', request.path) + uid = c.user._id if c.user_is_loggedin else '-' + g.log.error("E: %s U: %s FP: %s", error, uid, fullpath) + + class LoggingErrorReporter(Reporter): """ErrorMiddleware-compatible reporter that writes exceptions to g.log.""" @@ -125,15 +132,9 @@ class LoggingErrorReporter(Reporter): # exception_formatted is the output of traceback.format_exception_only exception = exc_data.exception_formatted[-1].strip() - # This is still within the RegistryManager middleware - fullpath = request.environ.get('FULLPATH', request.path) - uid = '-' - if c.user_is_loggedin: - uid = c.user._id - # First emit a single-line summary. This is great for grepping the # streaming log for errors. - g.log.error("E: %s U: %s FP: %s", exception, uid, fullpath) + write_error_summary(exception) text, extra = self.format_text(exc_data) # TODO: send this all in one burst so that error reports aren't