Skip to content

Commit 9a58e62

Browse files
SharadNair7Zac-HDnicoddemus
authored
Fixes issue #11314 - log_file_format does not default to log_format (#11444)
* Fixes issue #11314 - * Incorporated review comments for issue #11314 * Update changelog/11314.improvement.rst Co-authored-by: Bruno Oliveira <[email protected]> --------- Co-authored-by: Zac Hatfield-Dodds <[email protected]> Co-authored-by: Bruno Oliveira <[email protected]>
1 parent 8bac8d7 commit 9a58e62

File tree

4 files changed

+71
-7
lines changed

4 files changed

+71
-7
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ Segev Finer
345345
Serhii Mozghovyi
346346
Seth Junot
347347
Shantanu Jain
348+
Sharad Nair
348349
Shubham Adep
349350
Simon Gomizelj
350351
Simon Holesch

changelog/11314.improvement.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Logging to a file using the ``--log-file`` option will use ``--log-level``, ``--log-format`` and ``--log-date-format`` as fallback
2+
if ``--log-file-level``, ``--log-file-format`` and ``--log-file-date-format`` are not provided respectively.

src/_pytest/logging.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,13 @@ def add_option_ini(option, dest, default=None, type=None, **kwargs):
303303
add_option_ini(
304304
"--log-file-format",
305305
dest="log_file_format",
306-
default=DEFAULT_LOG_FORMAT,
306+
default=None,
307307
help="Log format used by the logging module",
308308
)
309309
add_option_ini(
310310
"--log-file-date-format",
311311
dest="log_file_date_format",
312-
default=DEFAULT_LOG_DATE_FORMAT,
312+
default=None,
313313
help="Log date format used by the logging module",
314314
)
315315
add_option_ini(
@@ -635,7 +635,9 @@ def __init__(self, config: Config) -> None:
635635
self.report_handler.setFormatter(self.formatter)
636636

637637
# File logging.
638-
self.log_file_level = get_log_level_for_setting(config, "log_file_level")
638+
self.log_file_level = get_log_level_for_setting(
639+
config, "log_file_level", "log_level"
640+
)
639641
log_file = get_option_ini(config, "log_file") or os.devnull
640642
if log_file != os.devnull:
641643
directory = os.path.dirname(os.path.abspath(log_file))

testing/logging/test_reporting.py

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ def test_foo():
7777
assert "warning text going to logger" not in stdout
7878
assert "info text going to logger" not in stdout
7979

80-
# The log file should contain the warning and the error log messages and
81-
# not the info one, because the default level of the root logger is
82-
# WARNING.
80+
# The log file should only contain the error log messages and
81+
# not the warning or info ones, because the root logger is set to
82+
# ERROR using --log-level=ERROR.
8383
assert os.path.isfile(log_file)
8484
with open(log_file, encoding="utf-8") as rfh:
8585
contents = rfh.read()
8686
assert "info text going to logger" not in contents
87-
assert "warning text going to logger" in contents
87+
assert "warning text going to logger" not in contents
8888
assert "error text going to logger" in contents
8989

9090

@@ -1331,3 +1331,62 @@ def test_foo():
13311331
result.stdout.re_match_lines(
13321332
[r"^[0-9-]{10} [0-9:]{8}.[0-9]{6}[+-][0-9\.]+; WARNING; text"]
13331333
)
1334+
1335+
1336+
def test_log_file_cli_fallback_options(pytester: Pytester) -> None:
1337+
"""Make sure that fallback values for log-file formats and level works."""
1338+
pytester.makepyfile(
1339+
"""
1340+
import logging
1341+
logger = logging.getLogger()
1342+
1343+
def test_foo():
1344+
logger.info('info text going to logger')
1345+
logger.warning('warning text going to logger')
1346+
logger.error('error text going to logger')
1347+
1348+
assert 0
1349+
"""
1350+
)
1351+
log_file = str(pytester.path.joinpath("pytest.log"))
1352+
result = pytester.runpytest(
1353+
"--log-level=ERROR",
1354+
"--log-format=%(asctime)s %(message)s",
1355+
"--log-date-format=%H:%M",
1356+
"--log-file=pytest.log",
1357+
)
1358+
assert result.ret == 1
1359+
1360+
# The log file should only contain the error log messages
1361+
# not the warning or info ones and the format and date format
1362+
# should match the formats provided using --log-format and --log-date-format
1363+
assert os.path.isfile(log_file)
1364+
with open(log_file, encoding="utf-8") as rfh:
1365+
contents = rfh.read()
1366+
assert re.match(r"[0-9]{2}:[0-9]{2} error text going to logger\s*", contents)
1367+
assert "info text going to logger" not in contents
1368+
assert "warning text going to logger" not in contents
1369+
assert "error text going to logger" in contents
1370+
1371+
# Try with a different format and date format to make sure that the formats
1372+
# are being used
1373+
result = pytester.runpytest(
1374+
"--log-level=ERROR",
1375+
"--log-format=%(asctime)s : %(message)s",
1376+
"--log-date-format=%H:%M:%S",
1377+
"--log-file=pytest.log",
1378+
)
1379+
assert result.ret == 1
1380+
1381+
# The log file should only contain the error log messages
1382+
# not the warning or info ones and the format and date format
1383+
# should match the formats provided using --log-format and --log-date-format
1384+
assert os.path.isfile(log_file)
1385+
with open(log_file, encoding="utf-8") as rfh:
1386+
contents = rfh.read()
1387+
assert re.match(
1388+
r"[0-9]{2}:[0-9]{2}:[0-9]{2} : error text going to logger\s*", contents
1389+
)
1390+
assert "info text going to logger" not in contents
1391+
assert "warning text going to logger" not in contents
1392+
assert "error text going to logger" in contents

0 commit comments

Comments
 (0)