在 Fastapi 中使用 Logger 模块进行基础日志记录
Source: Dev.to
(未提供需要翻译的正文内容。)
当应用在生产环境崩溃时
在生产环境中,应用程序崩溃,用户不确定接下来该怎么办。
去年我在一个项目中遇到了这种情况。作为程序员,我必须赶紧打开终端查看错误并开始调试。
这并不是一次愉快的经历——直到一位高级开发者向我介绍了捕获和记录错误。我对这些概念很熟悉,但从未在实际中使用过。加入合适的错误处理后,我的编程生活顺畅了许多。
捕获和记录错误的好处
- 更快的调试 – 日志告诉你发生了什么、何时发生、在哪里发生以及谁触发了它。
- 在生产环境中更好的错误可见性 – 在生产环境中你无法打印错误或附加调试器。日志是你唯一了解生产环境的窗口,这对远程服务器和被外部客户端使用的 API 尤为关键。
- 优雅的失败(更佳的用户体验) – 捕获错误可以让你返回有意义的 HTTP 响应,避免整个应用崩溃,并在出现故障时仍保持服务运行。
- 安全性和审计 – 日志帮助你检测可疑行为,追踪谁访问了什么,并审计敏感操作。
实践方法:FastAPI 中的日志记录与错误处理
下面是一个最小化的电商示例,演示如何在 FastAPI 应用中设置日志记录和错误处理。
文件结构

schemas.py – 定义 Pydantic 模型
from pydantic import BaseModel
from enum import Enum
class Status(str, Enum):
AVAILABLE = "AVAILABLE"
UNAVAILABLE = "UNAVAILABLE"
class ProductBase(BaseModel):
name: str
description: str
price: float
status: Status
class ProductCreate(ProductBase):
pass
class ProductUpdate(BaseModel):
name: str | None = None
description: str | None = None
price: float | None = None
status: Status | None = None
class Product(ProductBase):
id: int
main.py – 设置 FastAPI、日志记录和内存“数据库”
from fastapi import FastAPI, HTTPException, status, Response
import logging
from app.schemas import Product, ProductCreate, ProductUpdate
app = FastAPI()
# Configure logging – all logs go to app.log
logging.basicConfig(
filename="app.log",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)
# In‑memory list acting as a simple database
products: list[dict] = []
def fetch_product(product_id: int) -> dict | None:
"""Return a product dict matching the given id, or None if not found."""
for product in products:
if product["id"] == product_id:
return product
return None
端点 – 获取所有产品 & 单个产品
@app.get("/", response_model=list[Product])
def get_products():
"""Return the list of all products."""
try:
return products
except Exception as e:
logger.error(f"Unexpected error while getting products: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An unexpected error occurred while getting products",
)
@app.get("/{product_id}", response_model=Product)
def get_product(product_id: int):
"""Return a single product by its id."""
try:
product = fetch_product(product_id)
if not product:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Product not found"
)
return product
except Exception as e:
logger.error(f"Unexpected error while getting product {product_id}: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An unexpected error occurred while getting the product",
)
注意 try/except 块:
- 如果出现问题(例如
products未定义),异常会被记录,客户端收到的是通用的 500 响应,而不是原始的回溯信息。
示例:创建产品(演示用的故意错误)
@app.post("/", status_code=status.HTTP_201_CREATED, response_model=Product)
def create_product(product: ProductCreate):
"""Create a new product and add it to the in‑memory list."""
try:
# Uncomment the line below to simulate a coding error and see logging in action
# kkk # <-- intentional NameError for demonstration
product_data = product.model_dump()
product_data["id"] = len(products) + 1
products.append(product_data)
return product_data
except Exception as e:
logger.error(f"Unexpected error while creating product: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An unexpected error occurred while creating the product",
)
如果你插入语法或运行时错误(例如 kkk 行),日志记录机制会捕获并记录该错误,同时向客户端返回统一的 500 错误响应。
异常将被捕获,记录到 app.log,客户端将收到一个干净的 500 响应。
运行应用程序
-
创建虚拟环境(可选但推荐)。
-
安装依赖:
pip install fastapi uvicorn -
启动服务器:
uvicorn main:app --reload--reload标志在开发期间启用自动重新加载。 -
测试端点(例如,使用
curl、HTTPie、Postman 或浏览器)。 -
检查日志 – 打开
app.log,查看每次异常发生时记录的消息。
TL;DR
- Log 您需要了解的所有错误信息。
- Catch 在 API‑boundary 层捕获异常,以返回用户友好的响应。
- Never expose raw tracebacks 不要向终端用户暴露原始回溯信息;将其保存在供开发者使用的日志中。
实施这些实践可以使您的生产服务更可靠、易于调试且更安全。祝编码愉快!
Source:
FastAPI 基础日志记录
当在创建产品时出现错误,你可能会看到类似的消息:
error occurred while creating the product')
如何查看日志
- 运行
create_product接口。 - 打开生成的
app.log文件。
该文件会自动更新,显示任何新出现的错误。

这就是 FastAPI 中的基础错误日志记录。