Skip to content
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

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

Closed
ivasic opened this issue Dec 17, 2024 · 2 comments

Comments

@ivasic
Copy link

ivasic commented Dec 17, 2024

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.

@hynek
Copy link
Owner

hynek commented Dec 18, 2024

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.

@hynek hynek closed this as not planned Won't fix, can't repro, duplicate, stale Dec 18, 2024
@ivasic
Copy link
Author

ivasic commented Dec 18, 2024

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants