Skip to content

Commit

Permalink
Fixes issue #11314 - log_file_format does not default to log_format (#…
Browse files Browse the repository at this point in the history
…11444)

* Fixes issue #11314 -

* Incorporated review comments for issue #11314

* Update changelog/11314.improvement.rst

Co-authored-by: Bruno Oliveira <bruno@soliv.dev>

---------

Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
Co-authored-by: Bruno Oliveira <bruno@soliv.dev>
  • Loading branch information
3 people committed Sep 18, 2023
1 parent 8bac8d7 commit 9a58e62
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -345,6 +345,7 @@ Segev Finer
Serhii Mozghovyi
Seth Junot
Shantanu Jain
Sharad Nair
Shubham Adep
Simon Gomizelj
Simon Holesch
Expand Down
2 changes: 2 additions & 0 deletions changelog/11314.improvement.rst
@@ -0,0 +1,2 @@
Logging to a file using the ``--log-file`` option will use ``--log-level``, ``--log-format`` and ``--log-date-format`` as fallback
if ``--log-file-level``, ``--log-file-format`` and ``--log-file-date-format`` are not provided respectively.
8 changes: 5 additions & 3 deletions src/_pytest/logging.py
Expand Up @@ -303,13 +303,13 @@ def add_option_ini(option, dest, default=None, type=None, **kwargs):
add_option_ini(
"--log-file-format",
dest="log_file_format",
default=DEFAULT_LOG_FORMAT,
default=None,
help="Log format used by the logging module",
)
add_option_ini(
"--log-file-date-format",
dest="log_file_date_format",
default=DEFAULT_LOG_DATE_FORMAT,
default=None,
help="Log date format used by the logging module",
)
add_option_ini(
Expand Down Expand Up @@ -635,7 +635,9 @@ def __init__(self, config: Config) -> None:
self.report_handler.setFormatter(self.formatter)

# File logging.
self.log_file_level = get_log_level_for_setting(config, "log_file_level")
self.log_file_level = get_log_level_for_setting(
config, "log_file_level", "log_level"
)
log_file = get_option_ini(config, "log_file") or os.devnull
if log_file != os.devnull:
directory = os.path.dirname(os.path.abspath(log_file))
Expand Down
67 changes: 63 additions & 4 deletions testing/logging/test_reporting.py
Expand Up @@ -77,14 +77,14 @@ def test_foo():
assert "warning text going to logger" not in stdout
assert "info text going to logger" not in stdout

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


Expand Down Expand Up @@ -1331,3 +1331,62 @@ def test_foo():
result.stdout.re_match_lines(
[r"^[0-9-]{10} [0-9:]{8}.[0-9]{6}[+-][0-9\.]+; WARNING; text"]
)


def test_log_file_cli_fallback_options(pytester: Pytester) -> None:
"""Make sure that fallback values for log-file formats and level works."""
pytester.makepyfile(
"""
import logging
logger = logging.getLogger()
def test_foo():
logger.info('info text going to logger')
logger.warning('warning text going to logger')
logger.error('error text going to logger')
assert 0
"""
)
log_file = str(pytester.path.joinpath("pytest.log"))
result = pytester.runpytest(
"--log-level=ERROR",
"--log-format=%(asctime)s %(message)s",
"--log-date-format=%H:%M",
"--log-file=pytest.log",
)
assert result.ret == 1

# The log file should only contain the error log messages
# not the warning or info ones and the format and date format
# should match the formats provided using --log-format and --log-date-format
assert os.path.isfile(log_file)
with open(log_file, encoding="utf-8") as rfh:
contents = rfh.read()
assert re.match(r"[0-9]{2}:[0-9]{2} error text going to logger\s*", contents)
assert "info text going to logger" not in contents
assert "warning text going to logger" not in contents
assert "error text going to logger" in contents

# Try with a different format and date format to make sure that the formats
# are being used
result = pytester.runpytest(
"--log-level=ERROR",
"--log-format=%(asctime)s : %(message)s",
"--log-date-format=%H:%M:%S",
"--log-file=pytest.log",
)
assert result.ret == 1

# The log file should only contain the error log messages
# not the warning or info ones and the format and date format
# should match the formats provided using --log-format and --log-date-format
assert os.path.isfile(log_file)
with open(log_file, encoding="utf-8") as rfh:
contents = rfh.read()
assert re.match(
r"[0-9]{2}:[0-9]{2}:[0-9]{2} : error text going to logger\s*", contents
)
assert "info text going to logger" not in contents
assert "warning text going to logger" not in contents
assert "error text going to logger" in contents

0 comments on commit 9a58e62

Please sign in to comment.