Skip to content

Commit

Permalink
Merge pull request #948 from adamchainz/logcapture_propagate_false
Browse files Browse the repository at this point in the history
Fix logcapture plugin to capture output from non-propagating loggers,…
  • Loading branch information
jszakmeister committed Nov 28, 2015
2 parents 05882fa + 059e441 commit bee3081
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions nose/plugins/logcapture.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ def setupLoghandler(self):
if isinstance(handler, MyMemoryHandler):
root_logger.handlers.remove(handler)
root_logger.addHandler(self.handler)
# Also patch any non-propagating loggers in the tree
for logger in logging.Logger.manager.loggerDict.values():
if not getattr(logger, 'propagate', True) and hasattr(logger, "addHandler"):
for handler in logger.handlers[:]:
if isinstance(handler, MyMemoryHandler):
logger.handlers.remove(handler)
logger.addHandler(self.handler)
# to make sure everything gets captured
loglevel = getattr(self, "loglevel", "NOTSET")
root_logger.setLevel(getattr(logging, loglevel))
Expand Down
18 changes: 18 additions & 0 deletions unit_tests/test_logcapture_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,21 @@ class Dummy:
assert msg in ev
else:
assert msg.encode('utf-8') in ev

def test_non_propagating_loggers_handled(self):
c = LogCapture()
parser = OptionParser()
c.addOptions(parser, {})
options, args = parser.parse_args([])
c.configure(options, Config())

logger = logging.getLogger('foo.yes')
logger.propagate = False

c.start()
logger.debug("test message")
c.end()

records = c.formatLogRecords()
eq_(1, len(records))
assert records[0].startswith('foo.yes:'), records[0]

0 comments on commit bee3081

Please # to comment.