FastAPI 学习之路(六十)打造系统的日志输出
创始人
2024-12-28 16:09:58
0

我们要搭建日志系统,可以使用loguru,很不错的一个开源日志系统

pip install loguru

我们在common创建log.py,使用方式也很简单

import os import time  from loguru import logger  # 日志的路径 log_path = os.path.join(os.getcwd(), "logs") if not os.path.exists(log_path):     os.mkdir(log_path)  # 日志输出格式 log_path_error = os.path.join(log_path, f"{time.strftime('%Y-%m-%d')}_error.log")  logger.add(log_path_error, rotation="12:00", retention="5 days", enqueue=True)

使用起来也很简单

from common.log import logger  def create_user_method(db: Session, user: UserModel):     logger.info("创建用户开始了")     db_user = db.query(User).filter(User.email == user.email).first()     if db_user:         logger.info(f"用户:{user.email}已经存在了!")         return response(code=1, message="error", data="this user already exists")     from routers.users import get_password_hash     hashed_password = get_password_hash(user.password)     init_user = User(email=user.email, hashed_password=hashed_password)     db.add(init_user)     db.commit()     db.refresh(init_user)     logger.info("创建用户成功了")     return resp_200(data={"user": init_user.email}) 

启动后,如果logs文件夹不存在会自动创建该文件夹

看下请求的输出:(控制台中日志输出的颜色根据你使用的类型不同,对应的日志颜色也不同)

再看下文件中的输出:

也是按照固定的格式给我们输出的,还包含了我们是在哪一行输出的,什么方法中,方便我们后续的排查问题。

相关内容

热门资讯

技巧辅助挂!wepoker祈福... 技巧辅助挂!wepoker祈福有用吗,新518互游插件下载,项目教程(真实有挂)1、首先打开新518...
2026版辅助挂!wepoke... 2026版辅助挂!wepoker透视功能下载,创思维激k辅助工具,办法教程(有挂方针)1、玩家可以在...
2026版技巧!aapoker... 2026版技巧!aapoker真的假的,随意玩辅助器透视挂,模板教程(有挂神器)1、玩家可以在随意玩...
推出新举措!wepoker游戏... 推出新举措!wepoker游戏安装教程,创思维激k辅助器是真的假的,法门教程(有挂规律)运创思维激k...
随着!大菠萝789辅助器下载,... 随着!大菠萝789辅助器下载,山西扣点点辅助工具,项目教程(有挂详情)1、玩家可以在山西扣点点辅助工...
围绕透视问题!hhpoker怎... 围绕透视问题!hhpoker怎么开透视,山西扣点点辅助挂定制交易平台,模块教程(今日头条)1、用户打...
据报道!wpk透视是真的假的,... 据报道!wpk透视是真的假的,雀姬辅助器,技法教程(真的有挂)1、起透看视 雀姬辅助器辅助软件价格2...
黑科技技巧!德州局脚本,微信小... 黑科技技巧!德州局脚本,微信小程序免费黑科技,指引教程(有挂分析)微信小程序免费黑科技能透视中分为三...
现场直击!wepoker辅助器... 现场直击!wepoker辅助器下载,天天爱消除自动消除辅助,资料教程(有挂总结)1、首先打开天天爱消...
受玩家影响!安装不了wepok... 受玩家影响!安装不了wepoker,雀姬无限钻石辅助,模板教程(确实有挂)1、不需要AI权限,帮助你...