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

caplog.set_level has side effects if logger name is passed explicitly #60

Open
chekunkov opened this issue Mar 31, 2017 · 2 comments
Open

Comments

@chekunkov
Copy link

If I pass logger name to caplog.set_level like this

caplog.set_level(logging.ERROR, logger='package.utils')

then for all following test cases that logger will have level set to ERROR, which is not expected.

@chekunkov
Copy link
Author

I've fixed that with the following custom fixture, please let me know if it worth a PR

import logging
from pytest_catchlog import LogCaptureFixture


class CustomLogCaptureFixture(LogCaptureFixture):

    def __init__(self, item, monkeypatch):
        super().__init__(item)
        self._monkeypatch = monkeypatch

    def set_level(self, level, logger=None):
        obj = logger and logging.getLogger(logger) or self.handler
        self._monkeypatch.setattr(obj, 'level', logging._checkLevel(level))


@pytest.fixture()
def caplog(request, monkeypatch):
    """Custom caplog fixture that fixes set_level side effects"""
    return CustomLogCaptureFixture(request.node, monkeypatch)

@abusalimov
Copy link
Collaborator

@chekunkov Looks good at the first glance, thank you!

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

No branches or pull requests

2 participants