Minor internal improvements to logging's log_level (#6849)

This commit is contained in:
Daniel Hahler 2020-03-06 03:12:57 +01:00 committed by GitHub
commit fcd3fad03d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 10 deletions

View File

@ -8,11 +8,13 @@ from typing import Dict
from typing import Generator from typing import Generator
from typing import List from typing import List
from typing import Mapping from typing import Mapping
from typing import Optional
import pytest import pytest
from _pytest import nodes from _pytest import nodes
from _pytest.compat import nullcontext from _pytest.compat import nullcontext
from _pytest.config import _strtobool from _pytest.config import _strtobool
from _pytest.config import Config
from _pytest.config import create_terminal_writer from _pytest.config import create_terminal_writer
from _pytest.pathlib import Path from _pytest.pathlib import Path
@ -194,7 +196,12 @@ def pytest_addoption(parser):
"--log-level", "--log-level",
dest="log_level", dest="log_level",
default=None, default=None,
help="logging level used by the logging module", metavar="LEVEL",
help=(
"level of messages to catch/display.\n"
"Not set by default, so it depends on the root/parent log handler's"
' effective level, where it is "WARNING" by default.'
),
) )
add_option_ini( add_option_ini(
"--log-format", "--log-format",
@ -443,9 +450,7 @@ def caplog(request):
result._finalize() result._finalize()
def get_actual_log_level(config, *setting_names): def get_log_level_for_setting(config: Config, *setting_names: str) -> Optional[int]:
"""Return the actual logging level."""
for setting_name in setting_names: for setting_name in setting_names:
log_level = config.getoption(setting_name) log_level = config.getoption(setting_name)
if log_level is None: if log_level is None:
@ -453,7 +458,7 @@ def get_actual_log_level(config, *setting_names):
if log_level: if log_level:
break break
else: else:
return return None
if isinstance(log_level, str): if isinstance(log_level, str):
log_level = log_level.upper() log_level = log_level.upper()
@ -478,7 +483,7 @@ class LoggingPlugin:
"""Attaches to the logging module and captures log messages for each test. """Attaches to the logging module and captures log messages for each test.
""" """
def __init__(self, config): def __init__(self, config: Config) -> None:
"""Creates a new plugin to capture log messages. """Creates a new plugin to capture log messages.
The formatter can be safely shared across all handlers so The formatter can be safely shared across all handlers so
@ -498,9 +503,9 @@ class LoggingPlugin:
get_option_ini(config, "log_date_format"), get_option_ini(config, "log_date_format"),
get_option_ini(config, "log_auto_indent"), get_option_ini(config, "log_auto_indent"),
) )
self.log_level = get_actual_log_level(config, "log_level") self.log_level = get_log_level_for_setting(config, "log_level")
self.log_file_level = get_actual_log_level(config, "log_file_level") self.log_file_level = get_log_level_for_setting(config, "log_file_level")
self.log_file_format = get_option_ini(config, "log_file_format", "log_format") self.log_file_format = get_option_ini(config, "log_file_format", "log_format")
self.log_file_date_format = get_option_ini( self.log_file_date_format = get_option_ini(
config, "log_file_date_format", "log_date_format" config, "log_file_date_format", "log_date_format"
@ -513,7 +518,7 @@ class LoggingPlugin:
if log_file: if log_file:
self.log_file_handler = logging.FileHandler( self.log_file_handler = logging.FileHandler(
log_file, mode="w", encoding="UTF-8" log_file, mode="w", encoding="UTF-8"
) ) # type: Optional[logging.FileHandler]
self.log_file_handler.setFormatter(self.log_file_formatter) self.log_file_handler.setFormatter(self.log_file_formatter)
else: else:
self.log_file_handler = None self.log_file_handler = None
@ -563,7 +568,7 @@ class LoggingPlugin:
get_option_ini(config, "log_auto_indent"), get_option_ini(config, "log_auto_indent"),
) )
log_cli_level = get_actual_log_level(config, "log_cli_level", "log_level") log_cli_level = get_log_level_for_setting(config, "log_cli_level", "log_level")
self.log_cli_handler = log_cli_handler self.log_cli_handler = log_cli_handler
self.live_logs_context = lambda: catching_logs( self.live_logs_context = lambda: catching_logs(
log_cli_handler, formatter=log_cli_formatter, level=log_cli_level log_cli_handler, formatter=log_cli_formatter, level=log_cli_level