Skip to content

Commit

Permalink
logging: Add custom "CONSOLE" log level
Browse files Browse the repository at this point in the history
The console logging can generate a lot of output at the default level of
INFO, when it may be that all users care about is the step logging. To
deal with this, add a new custom log level called "CONSOLE" that can be
used. This level is below INFO, but above DEBUG.

When invoking pytest on the command line, the verbose flag "-vv", will
show steps (a synonym for "--log-cli-level=INFO"), "-vvv" will show
console input and output (a synonym for "--log-cli-level=CONSOLE") and
"-vvvv" will show almost everything (a synonym for
"--log-cli-level=DEBUG")

Signed-off-by: Joshua Watt <[email protected]>
[[email protected]: fix if → elif]
Signed-off-by: Rouven Czerwinski <[email protected]>
  • Loading branch information
JoshuaWatt authored and Emantor committed Apr 28, 2023
1 parent 016193a commit 7c42806
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
9 changes: 6 additions & 3 deletions labgrid/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def basicConfig(**kwargs):
root.handlers[0].setFormatter(StepFormatter())


logging.CONSOLE = logging.INFO - 1
logging.addLevelName(logging.CONSOLE, "CONSOLE")

# Use composition instead of inheritance
class StepFormatter:
def __init__(
Expand Down Expand Up @@ -101,11 +104,11 @@ def notify(self, event):

for part in parts:
data = self.vt100_replace_cr_nl(part)
logger.info(self.__create_message(event, data))
logger.log(logging.CONSOLE, self.__create_message(event, data))

elif state == "start" and step.args and "data" in step.args:
data = self.vt100_replace_cr_nl(step.args["data"])
logger.info(self.__create_message(event, data))
logger.log(logging.CONSOLE, self.__create_message(event, data))

def flush(self):
if self.lastevent is None:
Expand All @@ -114,7 +117,7 @@ def flush(self):
for source, logger in self.loggers.items():
data = self.vt100_replace_cr_nl(self.bufs[source])
if data:
logger.info(self.__create_message(self.lastevent, data))
logger.log(logging.CONSOLE, self.__create_message(self.lastevent, data))
self.bufs[source] = b""


Expand Down
23 changes: 13 additions & 10 deletions labgrid/pytestplugin/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ def set_cli_log_level(level):
config.option.log_cli_level = str(level)

verbosity = config.getoption("verbose")
if verbosity > 2: # enable with -vvv
if verbosity > 3: # enable with -vvvv
set_cli_log_level(logging.DEBUG)
elif verbosity > 2: # enable with -vvv
set_cli_log_level(logging.CONSOLE)
elif verbosity > 1: # enable with -vv
set_cli_log_level(logging.INFO)

Expand All @@ -39,22 +41,23 @@ def pytest_configure(config):
StepLogger.start()
config.add_cleanup(StepLogger.stop)

logging = config.pluginmanager.getplugin('logging-plugin')
logging.log_cli_handler.setFormatter(StepFormatter(
logging_plugin = config.pluginmanager.getplugin('logging-plugin')
logging_plugin.log_cli_handler.formatter.add_color_level(logging.CONSOLE, "blue")
logging_plugin.log_cli_handler.setFormatter(StepFormatter(
color=config.option.lg_colored_steps,
parent=logging.log_cli_handler.formatter,
parent=logging_plugin.log_cli_handler.formatter,
))
logging.log_file_handler.setFormatter(StepFormatter(
parent=logging.log_file_handler.formatter,
logging_plugin.log_file_handler.setFormatter(StepFormatter(
parent=logging_plugin.log_file_handler.formatter,
))

# Might be the same formatter instance, so get a reference for both before
# changing either
report_formatter = logging.report_handler.formatter
caplog_formatter = logging.caplog_handler.formatter
report_formatter = logging_plugin.report_handler.formatter
caplog_formatter = logging_plugin.caplog_handler.formatter

logging.report_handler.setFormatter(StepFormatter(parent=report_formatter))
logging.report_handler.setFormatter(StepFormatter(parent=caplog_formatter))
logging_plugin.report_handler.setFormatter(StepFormatter(parent=report_formatter))
logging_plugin.report_handler.setFormatter(StepFormatter(parent=caplog_formatter))

config.addinivalue_line("markers",
"lg_feature: marker for labgrid feature flags")
Expand Down

0 comments on commit 7c42806

Please sign in to comment.