我们要搭建日志系统,我们使用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. webpack--css、html 和 js 代码的常用处理

    前言 本文来总结下webpack中 css.js.html 代码常见的处理方式,学习笔记仅供参考. 正文 1.css样式文件处理 (1)提取css为一个单独的文件 在我们前面学习了webpack的基础 ...

  2. 【数据库】本地KEGG数据库如何拆分子库?

    目录 KEGG本地库文件 按物种拆分KEGG数据库 1.获得物种分类信息 2.获得物种分类的序列信息并建库 3.获得物种分类的K-ko对应文件 根据相似性原理,序列相似,功能相似,所有功能注释无非是用 ...

  3. zabbix 内网机器通信状态

    a=0 for xgip in ${xgipset[*]} do let a+=1 fping $xgip|grep alive >/dev/null if [ $a != 3 ];then i ...

  4. 【模板】无源汇有上下界可行流(网络流)/ZOJ2314

    先导知识 网络最大流 题目链接 https://vjudge.net/problem/ZOJ-2314 题目大意 多组数据,第一行为数据组数 \(T\). 对于每一组数据,第一行为 \(n,m\) 表 ...

  5. 24. 解决Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

    第一种: sudo vim /etc/resolv.conf 添加nameserver 8.8.8.8 第二种: /etc/apt/sources.list 的内容换成 deb http://old- ...

  6. k8s使用ceph的rbd作后端存储

    k8s使用rbd作后端存储 k8s里的存储方式主要有三种.分别是volume.persistent volumes和dynamic volume provisioning. volume: 就是直接挂 ...

  7. Shell学习(七)——sort、uniq、cut、wc命令详解

    Shell学习(七)--sort.uniq.cut.wc命令详解 转自:[1]linux sort,uniq,cut,wc命令详解 https://www.cnblogs.com/ggjucheng/ ...

  8. ajaxSubmit返回JSON格式

    开发时遇到根据不同情况返回错误提示信息的需求,用到了ajax中返回json格式数据的. 前台请求代码: <script type="text/javascript">  ...

  9. 【Python】【Module】json and pickle

    Python中用于序列化的两个模块 json     用于[字符串]和 [python基本数据类型] 间进行转换 pickle   用于[python特有的类型] 和 [python基本数据类型]间进 ...

  10. String类型和包装类型作为参数传递时,是属于值传递还是引用传递呢?

    原理知识: 如果参数类型是原始类型,那么传过来的就是这个参数的一个副本,也就是这个原始参数的值,这个跟之前所谈的传值是一样的.如果在函数中改变了副本的 值不会改变原始的值. 如果参数类型是引用类型,那 ...