Skip to content

Typer/Click invocation result doesn't capture stdout/stderr when using stdlib.LoggerFactory #686

@ivasic

Description

@ivasic

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions