I'm not sure if this is structlog or a click issue but trying here first.
For some reason, using stdlib.LoggerFactory prevents typer/click to capture stdout/stderr in the invocation result while a PrintLogger doesn't.
Here's a minimal example, any pointers to resolve/workaround this issue is most welcome.
# hello.py
import structlog
import typer
logger = structlog.get_logger()
# swapping LoggerFactory for a PrintLogger makes the test pass
structlog.configure(logger_factory=structlog.stdlib.LoggerFactory())
app = typer.Typer()
@app.command()
def hello(name: str) -> None:
logger.info("Hello, %s!", name)
if __name__ == "__main__":
app()
a test case:
from typer.testing import CliRunner
from hello import app
def test_log() -> None:
runner = CliRunner()
result = runner.invoke(app, ["hello"])
print(result.stdout)
assert "hello" in result.stdout
Running this test results in the assertion and result.stdout is completely empty.
I'm not sure if this is
structlogor aclickissue but trying here first.For some reason, using
stdlib.LoggerFactoryprevents typer/click to capture stdout/stderr in the invocation result while aPrintLoggerdoesn't.Here's a minimal example, any pointers to resolve/workaround this issue is most welcome.
a test case:
Running this test results in the assertion and
result.stdoutis completely empty.