【问题标题】:How to add timestamp to each request in uvicorn logs?如何为 uvicorn 日志中的每个请求添加时间戳?
【发布时间】:2020-07-16 12:10:41
【问题描述】:

当我使用 uvicorn 运行我的 FastAPI 服务器时:

uvicorn main:app --host 0.0.0.0 --port 8000 --log-level info

我运行服务器后得到的日志:

INFO:     Started server process [405098]
INFO:     Waiting for application startup.
INFO:     Connect to database...
INFO:     Successfully connected to the database!
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO:     122.179.31.158:54604 - "GET /api/hello_world?num1=5&num2=10 HTTP/1.1" 200 OK

如何获取时间戳以及请求日志记录?喜欢:

INFO:     "2020-07-16:23:34:78" - 122.179.31.158:54604 - "GET /api/hello_world?num1=5&num2=10 HTTP/1.1" 200 OK

【问题讨论】:

标签: python fastapi uvicorn


【解决方案1】:

你可以使用Uvicorn的LOGGING_CONFIG

import uvicorn
from uvicorn.config import LOGGING_CONFIG
from fastapi import FastAPI

app = FastAPI()

def run():
    LOGGING_CONFIG["formatters"]["default"]["fmt"] = "%(asctime)s [%(name)s] %(levelprefix)s %(message)s"
    uvicorn.run(app)

if __name__ == '__main__':
    run()

这将返回带有时间戳的 uvicorn 日志

2020-08-20 02:33:53,765 [uvicorn.error] INFO:     Started server process [107131]
2020-08-20 02:33:53,765 [uvicorn.error] INFO:     Waiting for application startup.
2020-08-20 02:33:53,765 [uvicorn.error] INFO:     Application startup complete.
2020-08-20 02:33:53,767 [uvicorn.error] INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

【讨论】:

  • LOGGING_CONFIG["formatters"]["default"]["datefmt"] = "%Y-%m-%d %H:%M:%S" 也很有用。
  • LOGGING_CONFIG["formatters"]["access"]["fmt"] = '%(asctime)s %(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s'
  • 这是我用的!谢谢! :)
【解决方案2】:

您可以创建一个 dict 记录器配置,并在您的主应用程序中使用 dictConfig 函数对其进行初始化。

#main.py

from logging.config import dictConfig
from config import log_config

from fastapi import FastAPI

dictConfig(log_config.sample_logger)

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

#config/log_config.py

sample_logger = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "access": {
            "()": "uvicorn.logging.AccessFormatter",
            "fmt": '%(levelprefix)s %(asctime)s :: %(client_addr)s - "%(request_line)s" %(status_code)s',
            "use_colors": True
        },
    },
    "handlers": {
        "access": {
            "formatter": "access",
            "class": "logging.StreamHandler",
            "stream": "ext://sys.stdout",
        },
    },
    "loggers": {
        "uvicorn.access": {
            "handlers": ["access"],
            "level": "INFO",
            "propagate": False
        },
    },
}

【讨论】:

    【解决方案3】:

    结合@Yagiz Degirmenci 和@HappyFace 的答案,我得到了这个代码。

    LOGGING_CONFIG["formatters"]["default"]["fmt"] = "%(asctime)s [%(name)s] %(levelprefix)s %(message)s"
    LOGGING_CONFIG["formatters"]["access"][
        "fmt"] = '%(asctime)s [%(name)s] %(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s'
    

    日志是这样的:

    2021-03-31 18:38:22,728 [uvicorn.error] INFO:     Started server process [21824]
    2021-03-31 18:38:22,729 [uvicorn.error] INFO:     Waiting for application startup.
    2021-03-31 18:38:22,729 [uvicorn.error] INFO:     Application startup complete.
    2021-03-31 18:38:22,729 [uvicorn.error] INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
    2021-03-31 18:38:26,359 [uvicorn.access] INFO:     127.0.0.1:51932 - "POST /limit HTTP/1.1" 200 OK
    

    【讨论】:

      【解决方案4】:

      能够通过不使用 uvicorn 命令从终端运行服务器来实现相同的目的。 但是通过使用 uvicorn 包的运行功能来运行服务器。

      在网上寻找解决方案时,我最终找到了这个issue,其中Dylan Anthony 使用uvicorn 包的运行功能编写了解决方案。

      虽然,知道如何使用 uvicorn 命令实现同样的效果仍然很好。

      【讨论】:

        猜你喜欢
        • 2010-09-25
        • 1970-01-01
        • 2012-12-24
        • 1970-01-01
        • 2015-08-30
        • 2017-07-14
        • 2017-12-01
        • 2020-08-18
        • 1970-01-01
        相关资源
        最近更新 更多