Skip to content

Commit

Permalink
Add options for customizing the logging experience
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmintz committed Mar 22, 2024
1 parent 12eda10 commit 4176abb
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions nose/plugins/logcapture.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
You can remove other installed logging handlers with the
``--logging-clear-handlers`` option."""
import logging
import os
import threading
from logging import Handler
from nose.plugins.base import Plugin
Expand Down Expand Up @@ -115,6 +116,18 @@ def options(self, parser, env):
help="Disable logging capture plugin. "
"Logging configuration will be left intact."
" [NOSE_NOLOGCAPTURE]")
parser.add_option(
"--capture-logs", "--capture_logs", action="store_true",
default=0, dest="capture_logs",
help="Enable logging capture plugin. "
"Logging configuration will be left intact."
" [NOSE_CAPTURELOGS]")
parser.add_option(
"--logging-init", action="store_true",
default=0, dest="logging_init",
help="Initialize standard logging configuration with:\n"
"logging.basicConfig(level)\n"
" [NOSE_LOGINIT]")
parser.add_option(
"--logging-format", action="store", dest="logcapture_format",
default=env.get('NOSE_LOGFORMAT') or self.logformat,
Expand Down Expand Up @@ -149,18 +162,28 @@ def options(self, parser, env):
help="Clear all other logging handlers")
parser.add_option(
"--logging-level", action="store",
default='NOTSET', dest="logcapture_level",
default='WARNING', dest="logcapture_level",
help="Set the log level to capture")

def configure(self, options, conf):
"""Configure plugin."""
self.conf = conf
if not options.logcapture or conf.loggingConfig:
self.enabled = False
self.logformat = options.logcapture_format
self.logdatefmt = options.logcapture_datefmt
self.clear = options.logcapture_clear
self.loglevel = options.logcapture_level
if not options.logcapture or conf.loggingConfig:
self.enabled = False
if (
"NOSE_CAPTURELOGS" in os.environ
and os.environ["NOSE_CAPTURELOGS"] == "1"
) or options.capture_logs:
self.enabled = True
if (
"NOSE_LOGINIT" in os.environ
and os.environ["NOSE_LOGINIT"] == "1"
) or options.logging_init:
logging.basicConfig(level=self.loglevel)
if options.logcapture_filters:
self.filters = options.logcapture_filters.split(',')

Expand Down

0 comments on commit 4176abb

Please sign in to comment.