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

Logging error in FastAPI implementation #90

Open
gclements-chwy opened this issue Aug 26, 2022 · 2 comments
Open

Logging error in FastAPI implementation #90

gclements-chwy opened this issue Aug 26, 2022 · 2 comments

Comments

@gclements-chwy
Copy link

gclements-chwy commented Aug 26, 2022

I am running into an issue with the logger implementation throwing the following error:

--- Logging error ---
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/logging/__init__.py", line 1083, in emit
    msg = self.format(record)
  File "/usr/local/lib/python3.9/logging/__init__.py", line 927, in format
    return fmt.format(record)
  File "/usr/local/lib/python3.9/site-packages/json_logging/__init__.py", line 243, in format
    log_object = self._format_log_object(record, request_util=_request_util)
  File "/app/./vet_onboarding_lambda/main.py", line 32, in _format_log_object
    json_log_object = super(JSONRequestLogFormatter, self)._format_log_object(record, request_util)
  File "/usr/local/lib/python3.9/site-packages/json_logging/__init__.py", line 276, in _format_log_object
    "remote_ip": request_adapter.get_remote_ip(request),
  File "/usr/local/lib/python3.9/site-packages/json_logging/framework/fastapi/implementation.py", line 108, in get_remote_ip
    return request.client.host
AttributeError: 'NoneType' object has no attribute 'host'

I'm using json-logging 1.3.0 and fastapi 0.80.0 (which uses Starlette 0.19.1) If you look in the source for Starlette it says that the property client on request can be None The getters in fastapi/implementation.py that use request.client need to make sure that client isn't None before using it.

Oh, I forgot to mention that my application is running in a Docker container.

@gclements-chwy
Copy link
Author

gclements-chwy commented Aug 26, 2022

In case anyone else runs into this issue there is a work around. You do have to poke around in json-logging internals.

from json_logging import _framework_support_map
from json_logging.framework.fastapi import FastAPIRequestAdapter

class MyFastAPIRequestAdapter(FastAPIRequestAdapter):
    def get_remote_ip(self, request: starlette.requests.Request):
        if request.client:
            return request.client.host
        return '-'

    def get_remote_port(self, request: starlette.requests.Request):
        if request.client:
            return request.client.port
        return '-'

_framework_support_map['fastapi']['request_adapter_class'] = MyFastAPIRequestAdapter

json_logging.init_fastapi(enable_json=True)

@bobbui
Copy link
Owner

bobbui commented Oct 4, 2022

PR welcome to patch fastapi adapter

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