Skip to content

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

Closed as not planned

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.

Activity

  1. hynek commented on Dec 18, 2024

    @hynek
    Owner

    stdlib.LoggerFactory uses stdlib's logging for output, so you must look into the integration between click and logging. It is not in structlog's power to do anything about it once it's inside logging.

  2. ivasic commented on Dec 18, 2024

    @ivasic
    Author

    Right, I thought so. What’s confusing me is that stdlib logging works fine when structlog is not in the chain. I was thinking maybe there’s something that bothers it in the way stdlib logger is configured by structlog but it you don’t have any ideas what it could be I’ll try looking into click.

    Thanks for the response in any case!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions