Minor internal improvements to logging's log_level (#6849)
This commit is contained in:
commit
fcd3fad03d
|
@ -8,11 +8,13 @@ from typing import Dict
|
|||
from typing import Generator
|
||||
from typing import List
|
||||
from typing import Mapping
|
||||
from typing import Optional
|
||||
|
||||
import pytest
|
||||
from _pytest import nodes
|
||||
from _pytest.compat import nullcontext
|
||||
from _pytest.config import _strtobool
|
||||
from _pytest.config import Config
|
||||
from _pytest.config import create_terminal_writer
|
||||
from _pytest.pathlib import Path
|
||||
|
||||
|
@ -194,7 +196,12 @@ def pytest_addoption(parser):
|
|||
"--log-level",
|
||||
dest="log_level",
|
||||
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(
|
||||
"--log-format",
|
||||
|
@ -443,9 +450,7 @@ def caplog(request):
|
|||
result._finalize()
|
||||
|
||||
|
||||
def get_actual_log_level(config, *setting_names):
|
||||
"""Return the actual logging level."""
|
||||
|
||||
def get_log_level_for_setting(config: Config, *setting_names: str) -> Optional[int]:
|
||||
for setting_name in setting_names:
|
||||
log_level = config.getoption(setting_name)
|
||||
if log_level is None:
|
||||
|
@ -453,7 +458,7 @@ def get_actual_log_level(config, *setting_names):
|
|||
if log_level:
|
||||
break
|
||||
else:
|
||||
return
|
||||
return None
|
||||
|
||||
if isinstance(log_level, str):
|
||||
log_level = log_level.upper()
|
||||
|
@ -478,7 +483,7 @@ class LoggingPlugin:
|
|||
"""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.
|
||||
|
||||
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_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_date_format = get_option_ini(
|
||||
config, "log_file_date_format", "log_date_format"
|
||||
|
@ -513,7 +518,7 @@ class LoggingPlugin:
|
|||
if log_file:
|
||||
self.log_file_handler = logging.FileHandler(
|
||||
log_file, mode="w", encoding="UTF-8"
|
||||
)
|
||||
) # type: Optional[logging.FileHandler]
|
||||
self.log_file_handler.setFormatter(self.log_file_formatter)
|
||||
else:
|
||||
self.log_file_handler = None
|
||||
|
@ -563,7 +568,7 @@ class LoggingPlugin:
|
|||
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.live_logs_context = lambda: catching_logs(
|
||||
log_cli_handler, formatter=log_cli_formatter, level=log_cli_level
|
||||
|
|
Loading…
Reference in New Issue