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

Hide console using structlog #313

Closed
nicolasvong opened this issue Apr 30, 2021 · 3 comments
Closed

Hide console using structlog #313

nicolasvong opened this issue Apr 30, 2021 · 3 comments

Comments

@nicolasvong
Copy link

Can structlog be used with pythonw to only log in file ?

Importing structlog fails when pythonw is used to run the python code because sys.stdout is None.
The error is raised in file structlog/_config.py when sys.stdout.isatty() is called.

Here is a simple test that reveals the issue.

# python try_import_structlog.pyw --> success
# pythonw try_import_structlog.pyw --> fail

with open("result.txt", "a") as file:
    try:
        import structlog
    except Exception as error:
        file.write(f"import failed: {error}")
    else:
        file.write("import succeeded")

https://stackoverflow.com/q/67315429/15792213

@hynek
Copy link
Owner

hynek commented Apr 30, 2021

Oh that's a bug. Would you mind changing the line to

    ConsoleRenderer(colors=_has_colorama and sys.stdout and sys.stdout.isatty()),

and see if everything else works? I don't have a test environment to verify that.

@nicolasvong
Copy link
Author

Oh that's a bug. Would you mind changing the line to

    ConsoleRenderer(colors=_has_colorama and sys.stdout and sys.stdout.isatty()),

and see if everything else works? I don't have a test environment to verify that.

Your correction resolved my import problem, and logs are correctly generated

@hynek hynek closed this as completed in 272cbe6 Apr 30, 2021
@hynek
Copy link
Owner

hynek commented Apr 30, 2021

Thank you!

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