""" This plugin captures logging statements issued during test execution. When an error or failure occurs, the captured log messages are attached to the running test in the test.capturedLogging attribute, and displayed with the error failure output. It is enabled by default but can be turned off with the option ``--nologcapture``.
You can filter captured logging statements with the ``--logging-filter`` option. If set, it specifies which logger(s) will be captured; loggers that do not match will be passed. Example: specifying ``--logging-filter=sqlalchemy,myapp`` will ensure that only statements logged via sqlalchemy.engine, myapp or myapp.foo.bar logger will be logged.
You can remove other installed logging handlers with the ``--logging-clear-handlers`` option. """
except ImportError: from io import StringIO
# @staticmethod else: inclusive.append(component)
"""returns whether this record should be printed""" # nothing to filter return True
# @staticmethod """return the bool of whether `record` starts with any item in `matchers`"""
return self._any_match(self.inclusive, record)
return False
pass # do nothing state = self.__dict__.copy() del state['lock'] return state self.__dict__.update(state) self.lock = threading.RLock()
""" Log capture plugin. Enabled by default. Disable with --nologcapture. This plugin captures logging statements issued during test execution, appending any output captured to the error or failure output, should the test fail or raise an error. """
"""Register commandline options. """ "--nologcapture", action="store_false", default=not env.get(self.env_opt), dest="logcapture", help="Disable logging capture plugin. " "Logging configuration will be left intact." " [NOSE_NOLOGCAPTURE]") "--logging-format", action="store", dest="logcapture_format", default=env.get('NOSE_LOGFORMAT') or self.logformat, metavar="FORMAT", help="Specify custom format to print statements. " "Uses the same format as used by standard logging handlers." " [NOSE_LOGFORMAT]") "--logging-datefmt", action="store", dest="logcapture_datefmt", default=env.get('NOSE_LOGDATEFMT') or self.logdatefmt, metavar="FORMAT", help="Specify custom date/time format to print statements. " "Uses the same format as used by standard logging handlers." " [NOSE_LOGDATEFMT]") "--logging-filter", action="store", dest="logcapture_filters", default=env.get('NOSE_LOGFILTER'), metavar="FILTER", help="Specify which statements to filter in/out. " "By default, everything is captured. If the output is too" " verbose,\nuse this option to filter out needless output.\n" "Example: filter=foo will capture statements issued ONLY to\n" " foo or foo.what.ever.sub but not foobar or other logger.\n" "Specify multiple loggers with comma: filter=foo,bar,baz.\n" "If any logger name is prefixed with a minus, eg filter=-foo,\n" "it will be excluded rather than included. Default: " "exclude logging messages from nose itself (-nose)." " [NOSE_LOGFILTER]\n") "--logging-clear-handlers", action="store_true", default=False, dest="logcapture_clear", help="Clear all other logging handlers") "--logging-level", action="store", default='NOTSET', dest="logcapture_level", help="Set the log level to capture")
"""Configure plugin. """ # Disable if explicitly disabled, or if logging is # configured via logging config file self.enabled = False self.filters = options.logcapture_filters.split(',')
# setup our handler with root logger if hasattr(root_logger, "handlers"): for handler in root_logger.handlers: root_logger.removeHandler(handler) for logger in list(logging.Logger.manager.loggerDict.values()): if hasattr(logger, "handlers"): for handler in logger.handlers: logger.removeHandler(handler) # make sure there isn't one already # you can't simply use "if self.handler not in root_logger.handlers" # since at least in unit tests this doesn't work -- # LogCapture() is instantiated for each test case while root_logger # is module global # so we always add new MyMemoryHandler instance # to make sure everything gets captured
"""Set up logging handler before test run begins. """
self.filters)
pass
"""Clear buffers and handlers before test. """
"""Clear buffers after test. """
"""Add captured log messages to failure output. """ return self.formatError(test, err)
"""Add captured log messages to error output. """ # logic flow copied from Capture.formatError test.capturedLogging = records = self.formatLogRecords() if not records: return err ec, ev, tb = err return (ec, self.addCaptureToErr(ev, records), tb)
return list(map(safe_str, self.handler.buffer))
return '\n'.join([safe_str(ev), ln('>> begin captured logging <<')] + \ records + \ [ln('>> end captured logging <<')]) |