-
-
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
LoggingPlugin: Support to customize log_file from hook #4752
Conversation
932f8db
to
ac8a186
Compare
Looks like this is not really based on features, but on master (or similar). Please rebase it on features, and squash your commits into a single one. |
ac8a186
to
f038795
Compare
doc/en/logging.rst
Outdated
@@ -198,6 +198,16 @@ option names are: | |||
* ``log_file_format`` | |||
* ``log_file_date_format`` | |||
|
|||
You can call ``set_log_filename()`` to customize log_file path dinamically:: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can call ``set_log_filename()`` to customize log_file path dinamically:: | |
You can call ``set_log_filename()`` to customize the log_file path dynamically:: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the review. The typo is fixed, and also commits are squashed and rebased.
3c8216a
to
fd5ce26
Compare
Question: |
i believe for consistency with old python we need to the function to be called in case we get a relative path, we need to choose what we use as base as well, |
About the parameter, it should just be |
@nicoddemus the parameter must support being cast to a Path, it must not be a Path |
fd5ce26
to
5235bb8
Compare
Codecov Report
@@ Coverage Diff @@
## features #4752 +/- ##
============================================
- Coverage 95.67% 95.67% -0.01%
============================================
Files 113 113
Lines 25041 25125 +84
Branches 2483 2491 +8
============================================
+ Hits 23959 24039 +80
- Misses 762 767 +5
+ Partials 320 319 -1
Continue to review full report at Codecov.
|
@RonnyPfannschmidt , @nicoddemus Thanks for the review notes. I have added some fixes:
Next I will add checking for abs/rel path and fix the base path if needed. |
5235bb8
to
9170608
Compare
src/_pytest/logging.py
Outdated
@@ -468,6 +469,16 @@ def _setup_cli_logging(self): | |||
log_cli_handler, formatter=log_cli_formatter, level=log_cli_level | |||
) | |||
|
|||
def set_log_path(self, fname): | |||
if isinstance(fname, pathlib.PurePath): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think just fname = Path(fname)
is enough instead of this check. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, did not understand why to cast vica versa?
At the end we will need str
type for fname
to work correctly within logging.FileHandler()
.
fname
parameter can be str
, or pathlib
type. If it is str
type everything is ok, logging.FileHandler()
will work. But if it is pathlib
type I would cast it to str
at beginning of this function.
Please correct me if I am wrong. Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, Just read: https://docs.python.org/3/library/logging.handlers.html#logging.FileHandler,
From 3.6 it supports Path() also. But I have an exception if filename in pathlib
type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe for compatibility reasons it is ok if the type is str
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should use pathlib and cast to str until we drop suport for python < 3.6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed
src/_pytest/logging.py
Outdated
if isinstance(fname, pathlib.PurePath): | ||
fname = str(fname) | ||
|
||
if not os.path.exists(os.path.dirname(fname)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following my advice above, it is just a matter of changing this to fname.parent.mkdir(exist_ok=True, parents=True)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed
fa0985a
to
77b0dd3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice for @Thisch to give his final say here. 👍
Also thanks a ton to @mitzkia for the contribution! 🤗 |
Thank you very much @mitzkia for working on this! @nicoddemus mentioned in #4707 (comment) that this feature should be marked as experimental. Not sure how this can be done. Maybe @nicoddemus can comment on what needs to be done to mark this feature as experimental. Please do not forget to add an unittest to this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test case for this method; possibly making a test with the code you mention in the docs to ensure we don't break it by accident in the future.
doc/en/logging.rst
Outdated
@@ -198,6 +198,16 @@ option names are: | |||
* ``log_file_format`` | |||
* ``log_file_date_format`` | |||
|
|||
You can call ``set_log_path()`` to customize the log_file path dynamically:: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is enough here to mention that this feature is experimental.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should add the example hook (that was part of 77b0dd3) once this feature is no longer experimental.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Thisch. I am agreed with you. I will take a note for myself about this. Is there a best practice to not forgot this kind of future changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would simply create a ticket for this.
@@ -468,6 +468,20 @@ def _setup_cli_logging(self): | |||
log_cli_handler, formatter=log_cli_formatter, level=log_cli_level | |||
) | |||
|
|||
def set_log_path(self, fname): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please write a docstring here given that this is now public. Also mention that this method is still experimental. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, fixed.
77b0dd3
to
71e3545
Compare
I have fixed latest review notes and also added working unit test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @mitzkia! We just need to update the imports and I think we are good to go!
testing/logging/test_reporting.py
Outdated
@@ -1002,3 +1002,49 @@ def pytest_sessionfinish(session, exitstatus): | |||
assert "sessionstart" in contents | |||
assert "runtestloop" in contents | |||
assert "sessionfinish" in contents | |||
|
|||
def test_log_set_path(testdir): | |||
from pathlib2 import Path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be from _pytest.pathlib import Path
so it works for all Python versions we support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, about that. Previously I already used that way (as you mentioned), just forgot. Soon I will fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
testing/logging/test_reporting.py
Outdated
testdir.makeconftest( | ||
""" | ||
import pytest | ||
from pathlib2 import Path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be from _pytest.pathlib import Path
so it works for all Python versions we support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks the review notes, I will fix this also
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
4b5ef37
to
bc3bdd4
Compare
I know about TravisCI failing, soon I will fix it. |
@mitzkia thanks for the followup, you are doing great, please sort things out at a pace you are most comfortable with |
eae2d87
to
9b5ad2e
Compare
Sorry, but I can not figure out what could be the last CI error on Windows, I have already tried many possible fixes, but none of them worked. Unfortunately I have not got such Windows environment to try it. Thanks a lot for the help. |
@mitzkia sure thing! The problem is that you are inlining the full path name into the source code:
And writing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @mitzkia!
- This patch allows to set log_file (path) from hook Signed-off-by: Thomas Hisch Signed-off-by: Andras Mitzki <andras.mitzki@balabit.com>
5bb3af3
to
e3824d2
Compare
@nicoddemus Thank you very much for the help and explanation. |
I have updated the branch. |
First of all thanks to @Thisch who allowed to make this PR possible.
Without this patch there was only one static way to customize log_file path: --log-file starter option.
This patch allows to customize log_file value also from hooks, which helps to create log-files dynamically even for each testcases.