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文件夹不存在会自动创建该文件夹

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

再看下文件中的输出:

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

相关内容

热门资讯

来临!hhpoker辅助器视频... 来临!hhpoker辅助器视频,wepoker辅助工具,一贯确实有挂(了解有挂)1、很好的工具软件,...
围绕透视问题!德普之星有辅助软... 围绕透视问题!德普之星有辅助软件吗,德州来玩辅助器,一贯真的是有挂(有人有挂)小薇(辅助器软件下载)...
第三方插件!aa poker辅... 第三方插件!aa poker辅助,xpoker辅助,一贯存在有挂(有挂助手);1、aa poker辅...
随着!德州局怎么透视,uupo... 随着!德州局怎么透视,uupoker有透视吗,总是是有挂(有挂分享)该软件可以轻松地帮助玩家将德州局...
最新消息!wepoker透视脚... 最新消息!wepoker透视脚本苹果版,hhpoker透视脚本,真是确实有挂(有挂规律)wepoke...
2026版软件!wepoker... 2026版软件!wepoker免费脚本咨询,wepoker免费透视,切实真的有挂(的确有挂)1、we...
2026版技巧!we-poke... 您好,we-poker有人玩吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302...
不少玩家反映!wepoker轻... 不少玩家反映!wepoker轻量版有透视吗,hhpoker透视脚本,一直确实有挂(有挂解惑)wepo...
现就发布提示!wepoker透... 现就发布提示!wepoker透视脚本免费app,wepoker软件靠谱么,好像确实有挂(有挂方式)1...
在玩家背景下!wepoker有... 在玩家背景下!wepoker有辅助器吗,wepoker养号规律,好像确实有挂(有人有挂);1、进入到...