Skip to content
Advertisement

fastapi logging error and swagger is not working

I’m making a rest API using fastapi.

I just wonder why I get below messages whenever I execute my server.

INFO:     Started server process [97154]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
--- Logging error ---
Traceback (most recent call last):
  File "/usr/lib/python3.10/logging/__init__.py", line 1100, in emit
    msg = self.format(record)
  File "/usr/lib/python3.10/logging/__init__.py", line 943, in format
    return fmt.format(record)
  File "/usr/lib/python3.10/logging/__init__.py", line 678, in format
    record.message = record.getMessage()
  File "/usr/lib/python3.10/logging/__init__.py", line 368, in getMessage
    msg = msg % self.args
TypeError: %d format: a real number is required, not str
Call stack:
  File "mYlOcAtIoN/src/main.py", line 14, in <module>
    uvicorn.run(app, host='0.0.0.0', port='8000')
  File "mYlOcAtIoN/venv/lib/python3.10/site-packages/uvicorn/main.py", line 461, in run
    server.run()
  File "mYlOcAtIoN/venv/lib/python3.10/site-packages/uvicorn/server.py", line 67, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "mYlOcAtIoN/venv/lib/python3.10/site-packages/uvicorn/server.py", line 84, in serve
    await self.startup(sockets=sockets)
  File "mYlOcAtIoN/venv/lib/python3.10/site-packages/uvicorn/server.py", line 174, in startup
    self._log_started_message(listeners)
  File "mYlOcAtIoN/venv/lib/python3.10/site-packages/uvicorn/server.py", line 215, in _log_started_message
    logger.info(
Message: 'Uvicorn running on %s://%s:%d (Press CTRL+C to quit)'
Arguments: ('http', '0.0.0.0', '8000')

And below is main.py.

import uvicorn as uvicorn

app = FastAPI()
# Skip some settings

if __name__ == '__main__':
    uvicorn.run(app, host='0.0.0.0', port='8000')

Also, I can’t get anything in swagger and redocs whereas I can get REST methods’ results.

Advertisement

Answer

You need to change your port to int and app object to str like this:

if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=8000)
Advertisement