我们要搭建日志系统,我们使用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.logs import logger
# 新建用户
@usersRouter.post("/users/", tags=["users"])
def create_user(user: UserCreate, db: Session = Depends(get_db)):
"""
- **email**: 用户的邮箱
- **password**: 用户密码
"""
logger.info("创建用户")
db_crest = get_user_emai(db, user.email)
user.password = get_password_hash(user.password)
if not db_crest:
logger.success("创建用户成功")
user=db_create_user(db=db, user=user)
return reponse(code=0,data={'user':user.email},message="success")
logger.error("账号:{}不能重复".format(user.email))
return reponse(data={'msg':"账号不能重复"},code=1,message="error")

在使用的地方去引入,我们去启动即可。

启动后,默认创建了这个文件

我们看下请求的输出

而且在控制台的输出会根据你用的类型不一样,对应的日志的颜色也是不一样的,我们看下文件的中的输出

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

代码存储https://gitee.com/liwanlei/fastapistuday

文章首发在公众号,欢迎关注。

FastAPI 学习之路(六十)打造系统的日志输出的更多相关文章

  1. FastAPI 学习之路(十六)Form表单

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

  2. FastAPI 学习之路(十二)接口几个额外信息和额外数据类型

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

  3. FastAPI 学习之路(十四)响应模型

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

  4. FastAPI 学习之路(十五)响应状态码

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

  5. FastAPI 学习之路(十八)表单与文件

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

  6. FastAPI 学习之路(十九)处理错误

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

  7. FastAPI 学习之路(十)请求体的字段

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

  8. hadoop生态系统学习之路(十)MR将结果输出到hbase

    之前讲了MR将结果输出到hdfs.hive.db,今天再给大家分享一下,怎样将结果输出到hbase. 首先,提一句,笔者在hadoop集群运行此MR的时候报了一个错误.是一个jar包的缘故,这个错误是 ...

  9. FastAPI 学习之路(二十)接口文档配置相关

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

随机推荐

  1. Codeforces 1010F - Tree(分治 NTT+树剖)

    Codeforces 题面传送门 & 洛谷题面传送门 神仙题. 首先我们考虑按照这题的套路,记 \(t_i\) 表示 \(i\) 上的果子数量减去其儿子果子数量之和,那么对于一个合法的放置果子 ...

  2. ACAM 题乱做

    之前做了不少 ACAM,不过没怎么整理起来,还是有点可惜的. 打 * 的是推荐一做的题目. I. *CF1437G Death DBMS 见 我的题解. II. *CF1202E You Are Gi ...

  3. 【豆科基因组】豇豆Cowpea,Vigna unguiculata [L.] Walp.基因组2019PJ

    目录 来源 结果 基因组大小估计 采用stitching方法组装 修改豇豆染色体编号 基因注释和重复DNA 豇豆遗传多样性 SNP和INDEL Vu03 上 4.2-Mb 染色体倒位的鉴定 与其他暖季 ...

  4. 自动添加shell脚本头部信息

    autocmd BufNewFile *.sh exec ":call AddTitleForShell()" function AddTitleForShell() call a ...

  5. 56-Remove Linked List Elements

    Remove Linked List Elements My Submissions QuestionEditorial Solution Total Accepted: 61924 Total Su ...

  6. 关于SQL中Union和Join的用法

    转自帘卷西风的专栏(http://blog.csdn.net/ljxfblog) https://blog.csdn.net/ljxfblog/article/details/52066006 Uni ...

  7. 逻辑学与Prolog学习笔记

    int a = 3 + 5; 很自然.如果Matrix a, b要加呢?没有运算符重载,a + b是不行的,只能add(a, b). int a = add(3, 5)也行.如果函数名可以用+呢?+( ...

  8. Postman 中 Pre-request Script 常用 js 脚本

    1. 生成一个MD5或SHA1加密的字符串str_md5,str_sha1 string1 = "123456"; var str_md5= CryptoJS.MD5(string ...

  9. k8s之ansible安装

    项目地址:https://github.com/easzlab/kubeasz #:先配置harbor #:利用脚本安装docker root@k8s-harbor1:~# vim docker_in ...

  10. @PropertySource配置的用法

    功能 加载指定的属性文件(*.properties)到 Spring 的 Environment 中.可以配合 @Value 和@ConfigurationProperties 使用. @Proper ...