litestar-org / litestar

Production-ready, Light, Flexible and Extensible ASGI API framework | Effortlessly Build Performant APIs
https://litestar.dev/
MIT License
5.64k stars 382 forks source link

Bug: passing wrapper_class to StructLoggingConfig causes Pydantic exception #1948

Closed kpetersen-rgare closed 1 year ago

kpetersen-rgare commented 1 year ago

Description

When trying to hook up structlog to starlite v1.51.12, I got the Pydantic exception below on starting up the app. I tried the same with the litestar v2 beta hello-world app, and it worked. I realize this may not get fixed in v1.51.x. Is there a suggested workaround?

URL to code causing the issue

https://github.com/kpetersen-rgare/litestar-hello-world/blob/structlog-logging-config-bug/main.py

MCVE

# see URL to code

Steps to reproduce

1. clone MRE repo and `poetry install`
2. `poetry run uvicorn main:app`
3. See error

Screenshots

"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"

Logs

Traceback (most recent call last):
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/starlite-hello-world-cQlwT1sN-py3.11/bin/uvicorn", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/starlite-hello-world-cQlwT1sN-py3.11/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/starlite-hello-world-cQlwT1sN-py3.11/lib/python3.11/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/starlite-hello-world-cQlwT1sN-py3.11/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/starlite-hello-world-cQlwT1sN-py3.11/lib/python3.11/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/starlite-hello-world-cQlwT1sN-py3.11/lib/python3.11/site-packages/uvicorn/main.py", line 410, in main
    run(
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/starlite-hello-world-cQlwT1sN-py3.11/lib/python3.11/site-packages/uvicorn/main.py", line 578, in run
    server.run()
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/starlite-hello-world-cQlwT1sN-py3.11/lib/python3.11/site-packages/uvicorn/server.py", line 61, in run
    return asyncio.run(self.serve(sockets=sockets))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/starlite-hello-world-cQlwT1sN-py3.11/lib/python3.11/site-packages/uvicorn/server.py", line 68, in serve
    config.load()
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/starlite-hello-world-cQlwT1sN-py3.11/lib/python3.11/site-packages/uvicorn/config.py", line 473, in load
    self.loaded_app = import_from_string(self.app)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/starlite-hello-world-cQlwT1sN-py3.11/lib/python3.11/site-packages/uvicorn/importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/me/Developer/kpetersen-rgare/litestar-hello-world/main.py", line 19, in <module>
    logging_config = StructLoggingConfig(
                     ^^^^^^^^^^^^^^^^^^^^
  File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for StructLoggingConfig
wrapper_class
  Protocols with non-method members don't support issubclass() (type=type_error)

Litestar Version

1.51.12

Platform

Funding

Fund with Polar

JacobCoffee commented 1 year ago

Hi @kpetersen-rgare! Thanks for the report. We have squashed a few bugs around structlog issues and we should backport these.

edited: I am a bad reader and shouldnt answer issues so late 🙃

kpetersen-rgare commented 1 year ago

Hey, thanks for the quick response. I realized a simple workaround is to create my own structlog config class from the BaseLoggingConfig class that does not inherit from pydantic's BaseModel:

class MyStructLoggingConfig(BaseLoggingConfig):
    def configure(self) -> "GetLogger":
        structlog.configure(
            processors=processors,
            wrapper_class=structlog.make_filtering_bound_logger(logging.DEBUG),
            logger_factory=logger_factory,
        )
        return structlog.get_logger
provinzkraut commented 1 year ago

@JacobCoffee can you take this one?

JacobCoffee commented 1 year ago

Hi yes, I will look into this starting tonight