fastapi 日志记录错误和 swagger 不起作用
fastapi logging error and swagger is not working
我正在休息 api 使用 fastapi。
我只是想知道为什么每次执行我的服务器时都会收到以下消息。
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')
下面是main.py
.
import uvicorn as uvicorn
app = FastAPI()
# Skip some settings
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port='8000')
此外,我无法在 swagger 和 redocs 中得到任何东西,而我可以获得 REST 方法的结果。
请帮忙
您需要将端口更改为 int 并将应用程序对象更改为 str,如下面的简单代码
试一试
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000)
我正在休息 api 使用 fastapi。
我只是想知道为什么每次执行我的服务器时都会收到以下消息。
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')
下面是main.py
.
import uvicorn as uvicorn
app = FastAPI()
# Skip some settings
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port='8000')
此外,我无法在 swagger 和 redocs 中得到任何东西,而我可以获得 REST 方法的结果。
请帮忙
您需要将端口更改为 int 并将应用程序对象更改为 str,如下面的简单代码 试一试
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000)