-
Notifications
You must be signed in to change notification settings - Fork 18
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
Handling of loggers with propagate=False #44
Comments
This seems to do the trick: @pytest.yield_fixture
def caplog(caplog):
import logging
restore = []
for logger in logging.Logger.manager.loggerDict.values():
try:
if not logger.propagate:
restore += [(logger, logger.propagate)]
logger.propagate = True
except AttributeError:
pass
yield caplog
for logger, value in restore:
logger.propagate = value |
It might be better to add the caplog handler to the loggers with |
Sound good! I wish I had more spare time time to investigate that... |
FWIW, this is the current function I am using: @pytest.yield_fixture
def caplog(caplog):
import logging
restore = []
for logger in logging.Logger.manager.loggerDict.values():
try:
if not logger.propagate:
logger.propagate = True
restore += [logger]
except AttributeError:
pass
yield caplog
for logger in restore:
logger.propagate = False |
dwaynebailey
added a commit
to dwaynebailey/pootle
that referenced
this issue
Aug 5, 2016
dwaynebailey
added a commit
to dwaynebailey/pootle
that referenced
this issue
Aug 5, 2016
dwaynebailey
added a commit
to dwaynebailey/pootle
that referenced
this issue
Aug 11, 2016
dwaynebailey
added a commit
to dwaynebailey/pootle
that referenced
this issue
Aug 12, 2016
dwaynebailey
added a commit
to dwaynebailey/pootle
that referenced
this issue
Aug 18, 2016
dwaynebailey
added a commit
to dwaynebailey/pootle
that referenced
this issue
Aug 19, 2016
dwaynebailey
added a commit
to dwaynebailey/pootle
that referenced
this issue
Aug 19, 2016
dwaynebailey
added a commit
to dwaynebailey/pootle
that referenced
this issue
Aug 19, 2016
dwaynebailey
added a commit
to dwaynebailey/pootle
that referenced
this issue
Aug 19, 2016
dwaynebailey
added a commit
to dwaynebailey/pootle
that referenced
this issue
Aug 19, 2016
dwaynebailey
added a commit
to dwaynebailey/pootle
that referenced
this issue
Aug 24, 2016
dwaynebailey
added a commit
to dwaynebailey/pootle
that referenced
this issue
Aug 24, 2016
dwaynebailey
added a commit
to dwaynebailey/pootle
that referenced
this issue
Aug 24, 2016
dwaynebailey
added a commit
to dwaynebailey/pootle
that referenced
this issue
Aug 24, 2016
dwaynebailey
added a commit
to dwaynebailey/pootle
that referenced
this issue
Aug 24, 2016
dwaynebailey
added a commit
to dwaynebailey/pootle
that referenced
this issue
Aug 24, 2016
My solution is: @contextlib.contextmanager
def caplog_for_logger(caplog, logger_name):
caplog.clear()
caplog.set_level(logging.CRITICAL) # Mute the root-logger
logger = logging.getLogger(logger_name)
logger.addHandler(caplog.handler)
yield
logger.removeHandler(caplog.handler)
def test_foobar(self, caplog):
with caplog_for_logger(caplog, 'my-logger-name):
# testing .... |
willianantunes
added a commit
to willianantunes/django-graphql-playground
that referenced
this issue
Apr 7, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is the recommended way to handle loggers that are configured with
propagate=False
, which appears to cause the message not to reach the caplog handler?I can manually change the
propagate
setting in the test (setup), but there might be a better way?!Could pytest-catchlog take care of this automatically, e.g. by attaching its handler to all loggers with
propagate=False
?Would it be feasible to have something like
caplog.with_logger
, where you could pass in a logger name and caplog would attach it's handler there directly?The text was updated successfully, but these errors were encountered: