Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add loglevel option to logcapture plugin and add associated test #493

Merged
merged 1 commit into from
Jan 27, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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