From 16e9aa2b28b7d72e0b699cc126b7d5610dcf7d29 Mon Sep 17 00:00:00 2001 From: Arach Tchoupani Date: Wed, 25 Jan 2012 20:28:27 -0500 Subject: [PATCH] add loglevel option to logcapture plugin and add associated test --- nose/plugins/logcapture.py | 8 +++++++- unit_tests/test_logcapture_plugin.py | 21 ++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/nose/plugins/logcapture.py b/nose/plugins/logcapture.py index da3fe5d3..9dba58f6 100644 --- a/nose/plugins/logcapture.py +++ b/nose/plugins/logcapture.py @@ -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. @@ -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(',') @@ -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. diff --git a/unit_tests/test_logcapture_plugin.py b/unit_tests/test_logcapture_plugin.py index 05f667fe..f53f2337 100644 --- a/unit_tests/test_logcapture_plugin.py +++ b/unit_tests/test_logcapture_plugin.py @@ -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() @@ -102,7 +118,6 @@ def runTest(self): c.beforeTest(mktest()) c.end() - if py27: expect = [""] else: @@ -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() @@ -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()