Open westhomas opened 5 years ago
I believe I am also experiencing this. I'm am not super familiar with the caplog fixture but if it's intended usage is to capture all logging calls during test execution then like OP I am only seeing logging calls int he capture stderr output of pytest and not in the caplog.records
or other related attributes of the fixture.
A simplified view of my application and test code
def setup_logger() -> None:
dct: Dict[str, Any] = {
"version": 1,
"formatters": {
"formatter": {
"class": "logging.Formatter",
"format": "%(name)s:%(levelname)s:%(message)s",
}
},
"handlers": {
"stream_handler": {
"class": "logging.StreamHandler",
"level": "DEBUG",
"formatter": "formatter",
}
},
"root": {"level": "ERROR", "handlers": ["stream_handler"]},
}
dictConfig(dct)
def main(args=None) -> None:
setup_logger()
logger: logging.Logger = logging.getLogger(__name__)
parser: argparse.ArgumentParser = setup_parser()
args: argparse.Namespace = parser.parse_args(args=args)
if args.verbose:
logger.setLevel(logging.DEBUG)
logger.debug(f"called {__name__} with arguments:")
for arg_name, arg_value in vars(args).items():
if arg_value is not None:
logger.debug(f"{arg_name}: {arg_value}")
input_geojson(args)
logger.debug(f"finished splitting geojson")
def test_main_logging(caplog, random_geojson_file):
cli.main(args=["--verbose", str(random_geojson_file)])
print(caplog.records)
assert 0
produces
=================================== FAILURES ===================================
______________________________ test_main_logging _______________________________
caplog = <_pytest.logging.LogCaptureFixture object at 0x7fe56fd4f790>
random_geojson_file = PosixPath('/tmp/pytest-of-ysebastien_wsl/pytest-41/test_main_logging0/random.geojson')
def test_main_logging(caplog, random_geojson_file):
cli.main(args=["--verbose", str(random_geojson_file)])
print(caplog.records)
> assert 0
E assert 0
tests/test_cli.py:61: AssertionError
----------------------------- Captured stdout call -----------------------------
[]
----------------------------- Captured stderr call -----------------------------
geojsplit.cli:DEBUG:called geojsplit.cli with arguments:
geojsplit.cli:DEBUG:geojson: /tmp/pytest-of-ysebastien_wsl/pytest-41/test_main_logging0/random.geojson
geojsplit.cli:DEBUG:verbose: True
geojsplit.cli:DEBUG:dry_run: False
geojsplit.cli:DEBUG:starting splitting with geojson /tmp/pytest-of-ysebastien_wsl/pytest-41/test_main_logging0/random.geojson
geojsplit.cli:DEBUG:successfully saved 5 features to /tmp/pytest-of-ysebastien_wsl/pytest-41/test_main_logging0/random_xaaaa.geojson
geojsplit.cli:DEBUG:finished splitting geojson
Able to "finagle" it by saving the root loggers handler (LogCaptureHandler) for future use:
root_handler = logging.root.handlers[0]
logging.config.dictConfig(LOGGING)
root_handler.setLevel(int(log_level))
root_handler.setFormatter(logging.root.handlers[0].formatter)
logging.root.addHandler(root_handler)
Also seeing this (pytest 6.2.1).
Seems possibly related to https://github.com/pytest-dev/pytest/issues/5997 (see: https://github.com/pytest-dev/pytest/issues/5997#issuecomment-589177669).
With the same logging setup as OP, I am not able to capture the log records, and since one of their handlers is console/stderr, figured that capsys.readouterr().out
might work, but that's an empty string too. I can see Captured stderr call
right in front of me but can't get to it...
Also possibly related to https://github.com/pytest-dev/pytest/issues/7335.
I'm attempting to capture the output of a custom logging configuration dict. But for some reason the once I use
logging.config.dictConfig
all the logging output is pushed into theCaptured stderr call
and I can't see it in thecaplog
fixture.How do I access this logging data?
pip list
of the virtual environment you are usingRunning this test gives me this output (notice the INFO log in the
Captured stderr call
section):