【发布时间】:2020-08-20 17:10:22
【问题描述】:
我有一个我想在其上添加 python 日志记录的 fastapi 应用程序。我按照基本教程添加了这个,但是这并没有添加 API,只是添加了 gunicorn 日志记录。
所以我有一个使用 docker build 托管的本地服务器,因此使用 docker-compose up 运行服务器并使用 api 客户端(Insomnia,类似于邮递员)测试我的端点。
下面是没有创建日志文件的代码,因此没有添加日志语句。
我的项目str如下:
project/
src/
api/
models/
users.py
routers/
users.py
main.py
logging.conf
"""
main.py Main is the starting point for the app.
"""
import logging
import logging.config
from fastapi import FastAPI
from msgpack_asgi import MessagePackMiddleware
import uvicorn
from api.routers import users
logger = logging.getLogger(__name__)
app = FastAPI(debug=True)
app.include_router(users.router)
@app.get("/check")
async def check():
"""Simple health check endpoint."""
logger.info("logging from the root logger")
return {"success": True}
另外,我使用的 gunicorn.conf 看起来像这样:
[program:gunicorn]
command=poetry run gunicorn -c /etc/gunicorn/gunicorn.conf.py foodgame_api.main:app
directory=/var/www/
autostart=true
autorestart=true
redirect_stderr=true
和gunicorn.conf.py一样
import multiprocessing
bind = "unix:/tmp/gunicorn.sock"
workers = multiprocessing.cpu_count() * 2 + 1
worker_class = "uvicorn.workers.UvicornWorker"
loglevel = "debug"
errorlog = "-"
capture_output = True
chdir = "/var/www"
reload = True
reload_engine = "auto"
accesslog = "-"
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'
这是我在 docker 上的上述 API 端点的输出终端:
有人可以在这里指导我吗?我是 FastApi 的新手,因此我们将不胜感激。
【问题讨论】:
-
似乎没有启用记录器。
-
@ArakkalAbu 你如何启用记录器?
-
也看loguru。
标签: python-3.x logging docker-compose gunicorn fastapi