Change reporting of unhandled logging

Unhandled logging messages generated from sources other than Trove's
tests were flooding the log with numerous messages that included
complete backtraces. These backtraces are over 150mb when you run tox
-e py27. Unfortunately, while they are an indication of a problem,
they are most often not things that can be fixed in the Trove project.

To fix this, at least in the short term, this change only reports
anything if the logged message is coming from the context of a test.

Also, this change disables back traces in the messages being reported
unless an optional enables_backtrace is set. By default it set to
False and can only be enabled with a code change (i.e. no
configuration setting).

Change-Id: I9452f03bdd5365991e3c4831aa4936de3713612e
Closes-Bug: 1521373
This commit is contained in:
Amrith Kumar 2015-11-30 19:29:06 -05:00
parent 1232641b36
commit 67adc87e92
2 changed files with 18 additions and 11 deletions

View File

@ -22,15 +22,17 @@ class DefaultRootHandler(logging.StreamHandler):
__handler = logging.StreamHandler()
__singleton = None
__info = None
__enable_backtrace = False
@classmethod
def activate(cls):
def activate(cls, enable_backtrace=False):
# leverage the singleton __handler which has an
# acquire() method to create a critical section.
cls.__handler.acquire()
if cls.__singleton is None:
cls.__singleton = DefaultRootHandler()
cls.__enable_backtrace = enable_backtrace
cls.__handler.release()
return cls.__singleton
@ -47,21 +49,26 @@ class DefaultRootHandler(logging.StreamHandler):
super(DefaultRootHandler, self).__init__()
def emit(self, record):
msg = ("[" + record.name + "]\n" +
self.format(record) + "\n" +
(("\tFrom: " + DefaultRootHandler.__info + "\n")
if DefaultRootHandler.__info
else (''.join(traceback.format_stack()))))
self.stream.write(msg)
self.flush()
if DefaultRootHandler.__info:
msg = ("*************************\n" +
"Unhandled message logged from " +
DefaultRootHandler.__info + ", " +
record.name + "\n")
if DefaultRootHandler.__enable_backtrace:
msg += ''.join(traceback.format_stack()) + "\n"
msg += "*************************\n"
self.stream.write(msg)
self.flush()
class DefaultRootLogger(object):
"""A root logger that uses the singleton handler"""
def __init__(self):
def __init__(self, enable_backtrace=False):
super(DefaultRootLogger, self).__init__()
handler = DefaultRootHandler.activate()
handler = DefaultRootHandler.activate(enable_backtrace=False)
handler.acquire()
if handler not in logging.getLogger('').handlers:

View File

@ -44,7 +44,7 @@ class TestCase(testtools.TestCase):
'TROVE_TESTS_UNMOCK_ONLY_UNIQUE', True))
cls._dangling_mocks = set()
root_logger.DefaultRootLogger()
root_logger.DefaultRootLogger(enable_backtrace=False)
@classmethod
def is_bool(cls, val):