Skip to content

Commit fcd3fad

Browse files
authored
Minor internal improvements to logging's log_level (#6849)
2 parents 9e8540f + ddc8edf commit fcd3fad

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/_pytest/logging.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
from typing import Generator
99
from typing import List
1010
from typing import Mapping
11+
from typing import Optional
1112

1213
import pytest
1314
from _pytest import nodes
1415
from _pytest.compat import nullcontext
1516
from _pytest.config import _strtobool
17+
from _pytest.config import Config
1618
from _pytest.config import create_terminal_writer
1719
from _pytest.pathlib import Path
1820

@@ -194,7 +196,12 @@ def add_option_ini(option, dest, default=None, type=None, **kwargs):
194196
"--log-level",
195197
dest="log_level",
196198
default=None,
197-
help="logging level used by the logging module",
199+
metavar="LEVEL",
200+
help=(
201+
"level of messages to catch/display.\n"
202+
"Not set by default, so it depends on the root/parent log handler's"
203+
' effective level, where it is "WARNING" by default.'
204+
),
198205
)
199206
add_option_ini(
200207
"--log-format",
@@ -443,17 +450,15 @@ def caplog(request):
443450
result._finalize()
444451

445452

446-
def get_actual_log_level(config, *setting_names):
447-
"""Return the actual logging level."""
448-
453+
def get_log_level_for_setting(config: Config, *setting_names: str) -> Optional[int]:
449454
for setting_name in setting_names:
450455
log_level = config.getoption(setting_name)
451456
if log_level is None:
452457
log_level = config.getini(setting_name)
453458
if log_level:
454459
break
455460
else:
456-
return
461+
return None
457462

458463
if isinstance(log_level, str):
459464
log_level = log_level.upper()
@@ -478,7 +483,7 @@ class LoggingPlugin:
478483
"""Attaches to the logging module and captures log messages for each test.
479484
"""
480485

481-
def __init__(self, config):
486+
def __init__(self, config: Config) -> None:
482487
"""Creates a new plugin to capture log messages.
483488
484489
The formatter can be safely shared across all handlers so
@@ -498,9 +503,9 @@ def __init__(self, config):
498503
get_option_ini(config, "log_date_format"),
499504
get_option_ini(config, "log_auto_indent"),
500505
)
501-
self.log_level = get_actual_log_level(config, "log_level")
506+
self.log_level = get_log_level_for_setting(config, "log_level")
502507

503-
self.log_file_level = get_actual_log_level(config, "log_file_level")
508+
self.log_file_level = get_log_level_for_setting(config, "log_file_level")
504509
self.log_file_format = get_option_ini(config, "log_file_format", "log_format")
505510
self.log_file_date_format = get_option_ini(
506511
config, "log_file_date_format", "log_date_format"
@@ -513,7 +518,7 @@ def __init__(self, config):
513518
if log_file:
514519
self.log_file_handler = logging.FileHandler(
515520
log_file, mode="w", encoding="UTF-8"
516-
)
521+
) # type: Optional[logging.FileHandler]
517522
self.log_file_handler.setFormatter(self.log_file_formatter)
518523
else:
519524
self.log_file_handler = None
@@ -563,7 +568,7 @@ def _setup_cli_logging(self):
563568
get_option_ini(config, "log_auto_indent"),
564569
)
565570

566-
log_cli_level = get_actual_log_level(config, "log_cli_level", "log_level")
571+
log_cli_level = get_log_level_for_setting(config, "log_cli_level", "log_level")
567572
self.log_cli_handler = log_cli_handler
568573
self.live_logs_context = lambda: catching_logs(
569574
log_cli_handler, formatter=log_cli_formatter, level=log_cli_level

0 commit comments

Comments
 (0)