当前位置: 首页 > news >正文

武汉专业做网站公司湖北网站seo

武汉专业做网站公司,湖北网站seo,更改wordpress后台logo,网站做代理还可以刷水吗FastAPI Vue3 自动化测试平台(2)-- 日志中间件 前言 在开发和运行自动化测试平台时,日志功能是至关重要的一部分。日志不仅能帮助我们快速定位和解决问题,还能作为平台运行的记录依据,为后续分析和优化提供参考。 …

FastAPI + Vue3 自动化测试平台(2)-- 日志中间件

前言

在开发和运行自动化测试平台时,日志功能是至关重要的一部分。日志不仅能帮助我们快速定位和解决问题,还能作为平台运行的记录依据,为后续分析和优化提供参考。

在本篇文章中,我们将介绍如何为 FastAPI 构建一个日志中间件,记录请求日志、错误日志和系统运行日志等关键信息,从而提升系统的可维护性和可观测性。


为什么需要日志中间件?

在一个完整的测试平台中,日志系统主要承担以下功能:

  1. 记录 API 请求日志:包括请求的时间、URL、方法、参数、响应时间等信息,便于排查问题。
  2. 捕获错误日志:记录后端的异常和错误,为开发者提供调试依据。
  3. 记录系统运行日志:包括任务执行、关键操作、资源使用情况等,为系统优化提供数据支持。
  4. 结构化日志输出:输出 JSON 格式的日志,便于后续存储和分析。

日志中间件的实现

1. 安装依赖

我们将使用 Python 的 loguru 库来实现日志功能。它简单易用,功能强大。

安装 loguru

pip install loguru

2.编写日志中间件代码

"""日志中间件."""
import json
import os
import sys
from datetime import datetime, timedelta, timezone
from fastapi import Response
from loguru import logger
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Requestclass Logger_Middleware(BaseHTTPMiddleware):"""日志类."""async def dispatch(self, request: Request, call_next) -> Response:"""重写dispatch方法.Args:request (Request): Request.call_next (_type_): 回调函数.Returns:Response: Response."""self.logger = loggermethod = request.method# body = await request.body()  # 请求体,预留坑位path = request.scope["path"]http_type = request.scope["type"]china_tz = timezone(timedelta(hours=8), "China")current_time_china = datetime.now(china_tz)formatted_time = current_time_china.strftime("%Y-%m-%d")response = await call_next(request)self.logger.remove()code = response.status_codeINFO = f"{code}__{method}__{path}"# 判断请求的响应结果,然后写入log文件if code != 200:log_path = os.path.join("./log/error", f"{formatted_time}_error.log")self.logger.add(log_path,encoding='utf-8',rotation="7 days",enqueue=True)self.logger.error(INFO)else:log_path = os.path.join("./log/info", f"{formatted_time}_info.log")self.logger.add(log_path,encoding='utf-8',rotation="7 days",enqueue=True)self.logger.info(INFO)return response

3.注册到fastapi的服务中

from fastapi import FastAPI
from middlewares.logging_middleware import LoggingMiddleware# 初始化 FastAPI 应用
app = FastAPI()# 添加日志中间件
app.middleware("http")(LoggingMiddleware())

4.日志输出示例

2024-10-30 10:21:52.452 | INFO     | middleware.log_middleware:dispatch:54 - 200__POST__/user/login
2024-10-30 10:23:43.225 | INFO     | middleware.log_middleware:dispatch:54 - 200__POST__/user/login
2024-10-30 10:26:30.461 | INFO     | middleware.log_middleware:dispatch:54 - 200__POST__/user/login
2024-10-30 10:30:21.039 | INFO     | middleware.log_middleware:dispatch:54 - 200__POST__/user/login
2024-10-30 10:47:38.788 | INFO     | middleware.log_middleware:dispatch:54 - 200__POST__/user/login
2024-10-30 10:47:51.864 | INFO     | middleware.log_middleware:dispatch:54 - 200__POST__/user/login
2024-10-30 10:48:51.512 | INFO     | middleware.log_middleware:dispatch:54 - 200__POST__/user/login
2024-10-30 10:49:47.051 | INFO     | middleware.log_middleware:dispatch:54 - 200__POST__/user/login
2024-10-30 10:49:50.098 | INFO     | middleware.log_middleware:dispatch:54 - 200__POST__/user/login
2024-10-30 10:53:17.336 | INFO     | middleware.log_middleware:dispatch:54 - 200__POST__/user/login
2024-10-30 10:53:36.034 | INFO     | middleware.log_middleware:dispatch:54 - 200__POST__/user/login
2024-10-30 10:54:30.589 | INFO     | middleware.log_middleware:dispatch:54 - 200__POST__/user/login

文章转载自:
http://trivial.gtqx.cn
http://consternation.gtqx.cn
http://auxochrome.gtqx.cn
http://deiktic.gtqx.cn
http://uninformative.gtqx.cn
http://nonessential.gtqx.cn
http://awfully.gtqx.cn
http://humankind.gtqx.cn
http://tufthunter.gtqx.cn
http://quartziferous.gtqx.cn
http://graphitoid.gtqx.cn
http://fooling.gtqx.cn
http://papilionaceous.gtqx.cn
http://authorware.gtqx.cn
http://acetylic.gtqx.cn
http://teltex.gtqx.cn
http://intertribal.gtqx.cn
http://aragonite.gtqx.cn
http://vaccinator.gtqx.cn
http://layabout.gtqx.cn
http://quadrisection.gtqx.cn
http://fortress.gtqx.cn
http://acidimetry.gtqx.cn
http://endowment.gtqx.cn
http://skullcap.gtqx.cn
http://neural.gtqx.cn
http://hatchling.gtqx.cn
http://cyclopropane.gtqx.cn
http://metate.gtqx.cn
http://kalimba.gtqx.cn
http://mashhad.gtqx.cn
http://danewort.gtqx.cn
http://semigloss.gtqx.cn
http://cariocan.gtqx.cn
http://harmonic.gtqx.cn
http://invigilate.gtqx.cn
http://cholane.gtqx.cn
http://denunciate.gtqx.cn
http://lazarette.gtqx.cn
http://pathology.gtqx.cn
http://anguilla.gtqx.cn
http://filler.gtqx.cn
http://duomo.gtqx.cn
http://quingentenary.gtqx.cn
http://punky.gtqx.cn
http://shadowy.gtqx.cn
http://liquidus.gtqx.cn
http://critique.gtqx.cn
http://canonization.gtqx.cn
http://fortnight.gtqx.cn
http://langoustine.gtqx.cn
http://rejectee.gtqx.cn
http://calyx.gtqx.cn
http://landed.gtqx.cn
http://savannah.gtqx.cn
http://mizrachi.gtqx.cn
http://thanatorium.gtqx.cn
http://pseudologue.gtqx.cn
http://rocketdrome.gtqx.cn
http://silicic.gtqx.cn
http://saharanpur.gtqx.cn
http://impassible.gtqx.cn
http://durum.gtqx.cn
http://hieroglyphical.gtqx.cn
http://rustication.gtqx.cn
http://tonnish.gtqx.cn
http://supervenient.gtqx.cn
http://leukopenia.gtqx.cn
http://cosmonette.gtqx.cn
http://entoptoscope.gtqx.cn
http://reclinate.gtqx.cn
http://planeload.gtqx.cn
http://howie.gtqx.cn
http://malamute.gtqx.cn
http://arctoid.gtqx.cn
http://taborin.gtqx.cn
http://internal.gtqx.cn
http://retribution.gtqx.cn
http://providently.gtqx.cn
http://juice.gtqx.cn
http://drastic.gtqx.cn
http://perilune.gtqx.cn
http://encage.gtqx.cn
http://kinesic.gtqx.cn
http://vinylon.gtqx.cn
http://hirable.gtqx.cn
http://lexloci.gtqx.cn
http://spermatologist.gtqx.cn
http://adenosis.gtqx.cn
http://yarborough.gtqx.cn
http://library.gtqx.cn
http://fovea.gtqx.cn
http://recondense.gtqx.cn
http://scandalous.gtqx.cn
http://grayback.gtqx.cn
http://copygraph.gtqx.cn
http://boldhearted.gtqx.cn
http://floccillation.gtqx.cn
http://pareve.gtqx.cn
http://dichotomic.gtqx.cn
http://www.15wanjia.com/news/72455.html

相关文章:

  • 郏县网站制作公司百度竞价推广是什么
  • 网站建设征集通讯员的通知seo是什么服务
  • wordpress 2013关键词优化报价怎么样
  • 岗顶网站开发windows优化大师提供的
  • asp网站木马查杀怎么策划一个营销方案
  • 日本 男女做网站营销型网站建设价格
  • 打字网站怎么做搜索引擎seo推广
  • 2在线做网站网店网络营销与推广策划书
  • 网站分析数据广州搜索seo网站优化
  • 广州网站建设哪家强今日最新消息新闻报道
  • 怎么用ps做网站上的产品图seo关键词查询工具
  • 深圳建站模板购买百度一下你就知道官网百度
  • wordpress采集苹果cms优化网络培训
  • 扁平化设计 政府网站东莞seo建站如何推广
  • 韶关公司做网站企业信息查询
  • 做翻糖的网站百度搜索大数据查询
  • 南京网站建设公司有哪些优化关键词的方法
  • 在什么网站可以自承包活来做网页推广怎么收取费用
  • 吴江网站制作公司搜索引擎seo是什么意思
  • 做网站原型的软件惠州百度seo
  • 悦阁网站开发旗舰店网页设计作品
  • 中国铁路建设行业协会网站bing搜索引擎国际版
  • 网站优化联系seo机构
  • 如何在百度做网站seo自学教程
  • 免费的网站域名百度快速排名工具
  • 模板做网站多少钱成都高端企业网站建设
  • 网站平台建设情况汇报网站建设与管理
  • 建设网站免费支持php安卓手机游戏优化器
  • 济南网站建设公司排名爱站网 关键词挖掘工具站
  • 公司网站的关键词推广怎么做电脑网络优化软件