Skip to content

Commit

Permalink
Merge remote branch 'arachtchoupani/logcapture_loglevel' into merge-493
Browse files Browse the repository at this point in the history
  • Loading branch information
jpellerin committed Jan 27, 2012
2 parents e25f68b + 16e9aa2 commit 5f66c5b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 7 additions & 1 deletion nose/plugins/logcapture.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ def options(self, parser, env):
"--logging-clear-handlers", action="store_true",
default=False, dest="logcapture_clear",
help="Clear all other logging handlers")
parser.add_option(
"--logging-level", action="store",
default='NOTSET', dest="logcapture_level",
help="Set the log level to capture")

def configure(self, options, conf):
"""Configure plugin.
Expand All @@ -161,6 +165,7 @@ def configure(self, options, conf):
self.logformat = options.logcapture_format
self.logdatefmt = options.logcapture_datefmt
self.clear = options.logcapture_clear
self.loglevel = options.logcapture_level
if options.logcapture_filters:
self.filters = options.logcapture_filters.split(',')

Expand All @@ -186,7 +191,8 @@ def setupLoghandler(self):
root_logger.handlers.remove(handler)
root_logger.addHandler(self.handler)
# to make sure everything gets captured
root_logger.setLevel(logging.NOTSET)
loglevel = getattr(self, "loglevel", "NOTSET")
root_logger.setLevel(getattr(logging, loglevel))

def begin(self):
"""Set up logging handler before test run begins.
Expand Down
21 changes: 18 additions & 3 deletions unit_tests/test_logcapture_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ def test_captures_logging(self):
eq_(1, len(c.handler.buffer))
eq_("Hello", c.handler.buffer[0].msg)

def test_loglevel(self):
c = LogCapture()
parser = OptionParser()
c.addOptions(parser, {})
options, args = parser.parse_args(['--logging-level', 'INFO'])
c.configure(options, Config())
c.start()
log = logging.getLogger("loglevel")
log.debug("Hello")
log.info("Goodbye")
c.end()
records = c.formatLogRecords()
eq_(1, len(c.handler.buffer))
eq_("Goodbye", c.handler.buffer[0].msg)
eq_("loglevel: INFO: Goodbye", records[0])

def test_clears_all_existing_log_handlers(self):
c = LogCapture()
parser = OptionParser()
Expand All @@ -102,7 +118,6 @@ def runTest(self):
c.beforeTest(mktest())
c.end()


if py27:
expect = ["<class 'nose.plugins.logcapture.MyMemoryHandler'>"]
else:
Expand Down Expand Up @@ -141,7 +156,7 @@ def test_logging_filter(self):
assert records[0].startswith('foo:'), records[0]
assert records[1].startswith('foo.x:'), records[1]
assert records[2].startswith('bar.quux:'), records[2]

def test_logging_filter_exclude(self):
env = {'NOSE_LOGFILTER': '-foo,-bar'}
c = LogCapture()
Expand All @@ -159,7 +174,7 @@ def test_logging_filter_exclude(self):
eq_(2, len(records))
assert records[0].startswith('foobar.something:'), records[0]
assert records[1].startswith('abara:'), records[1]

def test_logging_filter_exclude_and_include(self):
env = {'NOSE_LOGFILTER': 'foo,-foo.bar'}
c = LogCapture()
Expand Down

0 comments on commit 5f66c5b

Please sign in to comment.