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
| * ``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.
| 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.
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
| ) | ||
|
|
||
| def set_log_path(self, fname): | ||
| if isinstance(fname, pathlib.PurePath): |
There was a problem hiding this comment.
I think just fname = Path(fname) is enough instead of this check. 👍
There was a problem hiding this comment.
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.
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.
Maybe for compatibility reasons it is ok if the type is str
There was a problem hiding this comment.
we should use pathlib and cast to str until we drop suport for python < 3.6
| if isinstance(fname, pathlib.PurePath): | ||
| fname = str(fname) | ||
|
|
||
| if not os.path.exists(os.path.dirname(fname)): |
There was a problem hiding this comment.
Following my advice above, it is just a matter of changing this to fname.parent.mkdir(exist_ok=True, parents=True)
fa0985a to
77b0dd3
Compare
nicoddemus
left a comment
There was a problem hiding this comment.
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. |
nicoddemus
left a comment
There was a problem hiding this comment.
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.
| * ``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.
I think it is enough here to mention that this feature is experimental.
There was a problem hiding this comment.
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.
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.
I would simply create a ticket for this.
| log_cli_handler, formatter=log_cli_formatter, level=log_cli_level | ||
| ) | ||
|
|
||
| def set_log_path(self, fname): |
There was a problem hiding this comment.
Please write a docstring here given that this is now public. Also mention that this method is still experimental. 👍
77b0dd3 to
71e3545
Compare
|
I have fixed latest review notes and also added working unit test. |
nicoddemus
left a comment
There was a problem hiding this comment.
Thanks @mitzkia! We just need to update the imports and I think we are good to go!
| assert "sessionfinish" in contents | ||
|
|
||
| def test_log_set_path(testdir): | ||
| from pathlib2 import Path |
There was a problem hiding this comment.
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.
Sorry, about that. Previously I already used that way (as you mentioned), just forgot. Soon I will fix.
| testdir.makeconftest( | ||
| """ | ||
| import pytest | ||
| from pathlib2 import Path |
There was a problem hiding this comment.
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.
Thanks the review notes, I will fix this also
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 |
- 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.