bobbui / json-logging-python

Cloud-native distributed Python logging library to emit JSON log that can be easily indexed by logging infrastructure
Apache License 2.0
303 stars 62 forks source link

Initialisation error: "Can not call init more than once" [FastAPI] #106

Open SahandJ opened 5 months ago

SahandJ commented 5 months ago

I've installed and imported json_logging. I then initialised the module like this:

import json_logging

from app.routers.jobs import jobs

load_dotenv()

app = FastAPI()
app.include_router(jobs.router, tags=["jobs"])

json_logging.init_fastapi(enable_json=True)

but this returns the following error:

Traceback (most recent call last):
  File "/Users/sj/Applications/PyCharm Professional Edition.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1535, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sj/Applications/PyCharm Professional Edition.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/sj/Code/app/main.py", line 55, in <module>
    uvicorn.run("app.main:app", host="127.0.0.1", port=8081, reload=False)
  File "/Users/sj/Code/.venv/lib/python3.12/site-packages/uvicorn/main.py", line 587, in run
    server.run()
  File "/Users/sj/Code/.venv/lib/python3.12/site-packages/uvicorn/server.py", line 61, in run
    return asyncio.run(self.serve(sockets=sockets))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sj/Applications/PyCharm Professional Edition.app/Contents/plugins/python/helpers-pro/pydevd_asyncio/pydevd_nest_asyncio.py", line 135, in run
    return loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sj/Applications/PyCharm Professional Edition.app/Contents/plugins/python/helpers-pro/pydevd_asyncio/pydevd_nest_asyncio.py", line 238, in run_until_complete
    return f.result()
           ^^^^^^^^^^
  File "/Users/sj/.local/share/mise/installs/python/3.12.2/lib/python3.12/asyncio/futures.py", line 203, in result
    raise self._exception.with_traceback(self._exception_tb)
  File "/Users/sj/.local/share/mise/installs/python/3.12.2/lib/python3.12/asyncio/tasks.py", line 314, in __step_run_and_handle_results
    result = coro.send(None)
             ^^^^^^^^^^^^^^^
  File "/Users/sj/Code/.venv/lib/python3.12/site-packages/uvicorn/server.py", line 68, in serve
    config.load()
  File "/Users/sj/Code/.venv/lib/python3.12/site-packages/uvicorn/config.py", line 467, in load
    self.loaded_app = import_from_string(self.app)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sj/Code/.venv/lib/python3.12/site-packages/uvicorn/importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sj/.local/share/mise/installs/python/3.12.2/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/Users/sj/Code/app/main.py", line 16, in <module>
    json_logging.init_fastapi(enable_json=True)
  File "/Users/sj/Code/.venv/lib/python3.12/site-packages/json_logging/__init__.py", line 410, in init_fastapi
    __init(framework_name='fastapi', custom_formatter=custom_formatter, enable_json=enable_json)
  File "/Users/sj/Code/.venv/lib/python3.12/site-packages/json_logging/__init__.py", line 117, in __init
    raise RuntimeError("Can not call init more than once")
RuntimeError: Can not call init more than once

I'm not sure why it's being initialised more than once. Any advice?