-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
log_file_format does not default to log_format #11314
Comments
Hi @schaetzc - thanks for your detailed investigation and writeup! Your proposed solution sounds great, and I'd be happy to receive a PR for it 🤩 Due to the age of the problem I've tagged this as an enhancement rather than a bugfix, but looking forward to the patch either way. |
Hi @schaetzc - are you still working on this fix / enhancement? If not, maybe i can pitch in to fix this if that's ok. Regards |
Hello @SharadNair7, thank you for the offer, that would be a huge help! I wanted to create the pull request for a while now, but never found the time to work on it. I didn't even start yet. This will probably go on for the next weeks, so I'm really happy that you can pitch in. Looking forward to your pull request :) |
Thank you @schaetzc, for your response. I will work on this issue then. Regards |
…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]>
Closed by #11444 🎉 |
Problem
Pytest's documentation states (emphasis mine)
This works as intended with the
--log-cli-...
options: When I have a pytest.ini file withlog_cli = true
and runpytest --log-format "%(message)s"
the console uses the specified format, even though I did not specify--log-cli-format
, but the more general--log-format
.For the
--log-file-...
options, I interpreted the wording "equal" just the same as I interpreted "mirror and default to", so I was very surprised thatpytest --log-file log.txt --log-format "%(message)s"
did not change the log format oflog.txt
.I debugged into _pytest and found that the option is queried using
log_file_format = get_option_ini(config, "log_file_format", "log_format")
which indicates that my interpretation of the documentation is the intended behavior. However, contrary tolog_cli_format
, thelog_file_format
option is defined with adefault=DEFAULT_LOG_FORMAT
. This causesget_option_ini
to return the default forlog_file_format
in every case. The additionally specified fallbacklog_format
is never checked.From git blame it seems the commit "Merge the pytest-catchlog plugin" introduced the bug (?) 6 years ago, affecting every version from 3.3.0 till today (). But as the name suggests, that commit did not really change the code, but only directly included it into the _pytest project. So the bug is even older than that.
Minimal Reproducible Example
Here is a minimal script reproduce the unexpected behavior. I used Python 3.9 on Windows 10, but that should be irrelevant here.
Create an empty directory and add a file
test_logging.py
with contentThen run this powershell script in that directory. You should be able to run this script with minimal changes in a posix sh shell on Linux too (e.g.
python3
instead ofpython
andsource venv/bin/activate
instead ofvenv/Scripts/activate
).This is the relevant output from
pytest freeze
and
type log.txt
:As you can see, the custom log format wasn't used for the file. Instead, pytest's
DEFAULT_LOG_FORMAT
was used.When I specify
--log-file-format "%(message)s
instead, I get the expectedlog.txt
:Solution
Remove the default value for
--log-file-format
so that it falls back to--log-format
and only uses the default format if neither of them are defined. Same for--log-file-date-format
.If you want to do it like this, I can create a pull request for you. I only created this issue first, in case you wanted to keep the behavior; see below:
Alternative: officially declare the bug as a feature, update the documentation, and remove the dead fallbacks to the general
log_format
/log_date_format
options from the code.I would be really sad if you went with the latter option. But it would be understandable, because the bug is ancient and I couldn't find any complains about this yet.
The text was updated successfully, but these errors were encountered: