8
8
from typing import Generator
9
9
from typing import List
10
10
from typing import Mapping
11
+ from typing import Optional
11
12
12
13
import pytest
13
14
from _pytest import nodes
14
15
from _pytest .compat import nullcontext
15
16
from _pytest .config import _strtobool
17
+ from _pytest .config import Config
16
18
from _pytest .config import create_terminal_writer
17
19
from _pytest .pathlib import Path
18
20
@@ -194,7 +196,12 @@ def add_option_ini(option, dest, default=None, type=None, **kwargs):
194
196
"--log-level" ,
195
197
dest = "log_level" ,
196
198
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
+ ),
198
205
)
199
206
add_option_ini (
200
207
"--log-format" ,
@@ -443,17 +450,15 @@ def caplog(request):
443
450
result ._finalize ()
444
451
445
452
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 ]:
449
454
for setting_name in setting_names :
450
455
log_level = config .getoption (setting_name )
451
456
if log_level is None :
452
457
log_level = config .getini (setting_name )
453
458
if log_level :
454
459
break
455
460
else :
456
- return
461
+ return None
457
462
458
463
if isinstance (log_level , str ):
459
464
log_level = log_level .upper ()
@@ -478,7 +483,7 @@ class LoggingPlugin:
478
483
"""Attaches to the logging module and captures log messages for each test.
479
484
"""
480
485
481
- def __init__ (self , config ) :
486
+ def __init__ (self , config : Config ) -> None :
482
487
"""Creates a new plugin to capture log messages.
483
488
484
489
The formatter can be safely shared across all handlers so
@@ -498,9 +503,9 @@ def __init__(self, config):
498
503
get_option_ini (config , "log_date_format" ),
499
504
get_option_ini (config , "log_auto_indent" ),
500
505
)
501
- self .log_level = get_actual_log_level (config , "log_level" )
506
+ self .log_level = get_log_level_for_setting (config , "log_level" )
502
507
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" )
504
509
self .log_file_format = get_option_ini (config , "log_file_format" , "log_format" )
505
510
self .log_file_date_format = get_option_ini (
506
511
config , "log_file_date_format" , "log_date_format"
@@ -513,7 +518,7 @@ def __init__(self, config):
513
518
if log_file :
514
519
self .log_file_handler = logging .FileHandler (
515
520
log_file , mode = "w" , encoding = "UTF-8"
516
- )
521
+ ) # type: Optional[logging.FileHandler]
517
522
self .log_file_handler .setFormatter (self .log_file_formatter )
518
523
else :
519
524
self .log_file_handler = None
@@ -563,7 +568,7 @@ def _setup_cli_logging(self):
563
568
get_option_ini (config , "log_auto_indent" ),
564
569
)
565
570
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" )
567
572
self .log_cli_handler = log_cli_handler
568
573
self .live_logs_context = lambda : catching_logs (
569
574
log_cli_handler , formatter = log_cli_formatter , level = log_cli_level
0 commit comments