Skip to content
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

#8711: Handle disabled logging in 'caplog.set_level' and 'caplog.at_level' #8758

Merged

Conversation

alexlambson
Copy link
Contributor

Forces requested caplog logging levels to be enabled if they were disabled via logging.disable()

[attr-defined] mypy error ignored in logging.py because there were existing errors with the imports
and loggin.Logger.manager is an attr set at runtime. Since it's in the standard lib I can't really fix that.

Ignored an attr-defined error in src/_pytest/config/__init__.py because the re-export is necessary.

Closes #8711


:return int: The original disabled logging level.
"""
original_disable_level: int = logger_obj.manager.disable # type: ignore[attr-defined]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linter doesn't like runtime attributes, so the ignore is needed to the best of my knowledge.

src/_pytest/logging.py Outdated Show resolved Hide resolved
@@ -448,6 +488,9 @@ def set_level(self, level: Union[int, str], logger: Optional[str] = None) -> Non
if self._initial_handler_level is None:
self._initial_handler_level = self.handler.level
self.handler.setLevel(level)
initial_disabled_logging_level = self.force_enable_logging(level, logger_obj)
if self._initial_disabled_logging_level is None:
self._initial_disabled_logging_level = initial_disabled_logging_level
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could remove this magic for a parameter if magic is frowned upon.

@alexlambson
Copy link
Contributor Author

Just bumping / following up on this because it's popped up for me again in a project.

Copy link
Contributor

@andrewdotn andrewdotn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sending this in! I’m new here, so I hope my comments are helpful.

src/_pytest/logging.py Outdated Show resolved Hide resolved
testing/logging/test_fixture.py Outdated Show resolved Hide resolved
src/_pytest/logging.py Outdated Show resolved Hide resolved
src/_pytest/logging.py Outdated Show resolved Hide resolved
def set_level(self, level: Union[int, str], logger: Optional[str] = None) -> None:
"""Set the level of a logger for the duration of a test.

.. versionchanged:: 3.4
The levels of the loggers changed by this function will be
restored to their initial values at the end of the test.

Will enable the requesed logging level if it was disabled via :meth:`logging.disable`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Will enable the requesed logging level if it was disabled via :meth:`logging.disable`.
Will enable the requested logging level if it was disabled via :meth:`logging.disable`.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess my "t" finger was tired that day.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@alexlambson
Copy link
Contributor Author

Thanks for sending this in! I’m new here, so I hope my comments are helpful.

Thanks for the review. I'll fix these in the morning :)

@alexlambson alexlambson force-pushed the issue-8711-handle-disabled-logging branch from ec6f9d0 to 3537eaf Compare November 6, 2021 01:02
alexlambson added a commit to alexlambson/pytest that referenced this pull request Nov 6, 2021
- Resolves issues from review provided by https://github.com/andrewdotn

- Fixed spelling errors

- Improved logic in `force_enable_logging`

- Make a fixture to un-disable logging after a test so that logging will be un-disabled even if the test fails due to an assertion error.
The fixture is in `test_fixture.py` because we can't `import logging` in `conftest.py` because there is a module called `logging` in the same folder.

- Mypy implicit import error. I can't commit without this.

Issue: pytest-dev#8711
PR: pytest-dev#8758
@alexlambson
Copy link
Contributor Author

alexlambson commented Nov 6, 2021

The code coverage seems to not look into those string-based tests. Most of the untested lines are actually tested. Should I just write direct function tests?

alexlambson added a commit to alexlambson/pytest that referenced this pull request Nov 11, 2021
- Adds test coverage for `force_enable_logging` with a string `level`. Fixes [code coverage check](https://github.com/pytest-dev/pytest/pull/8758/checks?check_run_id=4123920877)

Issue: pytest-dev#8711
PR: pytest-dev#8758
alexlambson added a commit to alexlambson/pytest that referenced this pull request Nov 11, 2021
- Adds test coverage for `force_enable_logging` with a string `level`. Fixes [code coverage check](https://github.com/pytest-dev/pytest/pull/8758/checks?check_run_id=4123920877)

Issue: pytest-dev#8711
PR: pytest-dev#8758
@alexlambson alexlambson force-pushed the issue-8711-handle-disabled-logging branch from 377d826 to e52ee7c Compare November 11, 2021 05:49
@alexlambson
Copy link
Contributor Author

Fixed test coverage

@blerrgh
Copy link

blerrgh commented May 17, 2023

What happened to this? Would be a super handy feature to have.

@alexlambson
Copy link
Contributor Author

Mainly just waiting for approval

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @alexlambson,

Really sorry this fell through the tracks and got forgotten for so long, I appreciate the recent pings.

I left a few minor comments, the PR is really well written, including the tests.

If you could just rebase it and fix the comments I left, I will be glad to get this merged.

👍

src/_pytest/logging.py Outdated Show resolved Hide resolved
src/_pytest/logging.py Outdated Show resolved Hide resolved
testing/logging/test_fixture.py Show resolved Hide resolved
src/_pytest/logging.py Outdated Show resolved Hide resolved
Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @alexlambson,

Really sorry this fell through the tracks and got forgotten for so long, I appreciate the recent pings.

I left a few minor comments, the PR is really well written, including the tests.

If you could just rebase it and fix the comments I left, I will be glad to get this merged.

👍

Alex Lambson and others added 4 commits May 17, 2023 21:25
…ytest-dev#8711

Forces requested `caplog` logging levels to be enabled if they were disabled via `logging.disable()`

`[attr-defined]` mypy error ignored in `logging.py` because there were existing errors with the imports
and `loggin.Logger.manager` is an attr set at runtime. Since it's in the standard lib I can't really fix that.

Ignored an attr-defined error in `src/_pytest/config/__init__.py` because the re-export is necessary.

Issue: pytest-dev#8711
- Resolves issues from review provided by https://github.com/andrewdotn

- Fixed spelling errors

- Improved logic in `force_enable_logging`

- Make a fixture to un-disable logging after a test so that logging will be un-disabled even if the test fails due to an assertion error.
The fixture is in `test_fixture.py` because we can't `import logging` in `conftest.py` because there is a module called `logging` in the same folder.

- Mypy implicit import error. I can't commit without this.

Issue: pytest-dev#8711
PR: pytest-dev#8758
- Adds test coverage for `force_enable_logging` with a string `level`. Fixes [code coverage check](https://github.com/pytest-dev/pytest/pull/8758/checks?check_run_id=4123920877)

Issue: pytest-dev#8711
PR: pytest-dev#8758
- Address review ad rebase to latest from main
- Make `force_enable_logging` private.

Issue: pytest-dev#8711
PR: pytest-dev#8758
@alexlambson alexlambson force-pushed the issue-8711-handle-disabled-logging branch from e52ee7c to 7f89996 Compare May 18, 2023 03:42
@alexlambson
Copy link
Contributor Author

alexlambson commented May 18, 2023

No worries @nicoddemus, pytest is a busy project.

I am curious why that diff pipeline is failing? I covered my code fairly well I thought. nvm, it was thinking.

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @alexlambson!

@nicoddemus nicoddemus merged commit ba32a3b into pytest-dev:main May 18, 2023
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle logging.disable() in caplog.set_level and caplog.at_level
4 participants