Add functionality for setting levels
Zeitsperre opened this issue · 8 comments
Hi, I realize that this is a small plugin, but I'd like to ask if there's any interest in allowing for levels to be set identical to the pytest-native caplog.
For my use case, the following example fails, since events at all levels are captured by caplog despite setting a level:
def test_logging(self, caplog): # caplog from pytest-loguru
caplog.set_level(logging.WARNING, logger="my_lib")
logging_examples() # logs events at all levels
assert ("my_lib.utils.common", 10, "1") not in caplog.record_tuples
assert ("my_lib.utils.common", 40, "4") in caplog.record_tuples
@Zeitsperre I would be happy to receive a PR with that enhancement.
I'm relatively new to logging generally and can't say I know how that would be done. If you had some pointers on where one would start to look into this, I'll take a stab at it though!
@Zeitsperre The best way to experiment would be to make a project that doesn't load the pytest-loguru
requirement, copy the fixture here into conftest.py
and then breakpoint in your test function to see what exactly gets set by caplog.set_level
in caplog. I guess that the thing that gets set can be read in the fixture in conftest.py
to control what gets output.
@Zeitsperre I asked upstream and I have checked in some changes to the main branch that should do what you want. Please can you try specifying this repo as a direct GitHub requirement in your project (so you get latest on main branch) and confirm it works for you.
Hi @mcarans, that's amazing! I have tests ready for this in a PR I'm working on in another project. Can confirm for you in the next day or so. Thanks!
@Zeitsperre Did you get a chance to check if this works for you?
I did, and it works!
def test_logging_with_caplog_levels(self, caplog):
caplog.set_level(logging.CRITICAL)
_logging_examples()
assert ("my_lib.testing._utils", 50, "5") in caplog.record_tuples
assert ("my_lib.testing._utils", 40, "4") not in caplog.record_tuples
Thanks again for this!
Version 0.2.0 released with this change. @Zeitsperre please reopen if you find any issues.