Hide console using structlog

86 Views Asked by At

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://github.com/hynek/structlog/issues/313

1

There are 1 best solutions below

0
On

This is a bug in structlog that will be fixed with next release! I think it might be out as soon as this weekend.