当前位置:网站首页>Flask框架配置loguru日志库
Flask框架配置loguru日志库
2022-07-06 09:28:00 【叻斯哩_LeslieWu】
项目场景:
loguru日志库是一个很不错的第三方日志库。它即插即用,具备多种方式滚动日志、自动压缩日志文件、定时删除等功能。除此之外,多线程安全、高亮日志、日志告警等功能也不在话下。
目前项目采用的是flask框架,以往采用的是系统原生的logging,想看看能否把loguru整合到flask里。
问题描述
通过网上查阅资料,找到了一些github上面分享配置的方法,不过在配置了rotation参数后(日志按照时间或大小,满足一定的条件切割日志),出现日志写入的进程问题。
PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。
原因分析:
原因初步分析,rotation参数,作用是一旦日志文件大小超过指定大小,就会产生新的日志文件。在修改原来日志文件的名字的时候,flask应用正在写入当前日志文件,导致无法操作。
解决方案:
给初始化的日志的配置参数LOGFILE,添加{time}
LOGFILE = "./files/logs/log_{time}.log"
之后就可以顺利的在flask中使用loguru,并且日志会根据对应的参数生效。
下面分享一下关于rotation参数的使用指南:
按时间滚动
比如按时间滚动,我们只需要在 logger.add 参数中添加一个 rotation 参数:
from loguru import logger
logger.add("file_2.log", rotation="12:00") # 每天12:00会创建一个新的文件
logger.debug("That's it, beautiful and simple logging!")
这样,如果当前时间过了这个设定的时间,它就会生成一个新的日志文件。如果没有则使用原来的日志文件:

如图所示,过了设定的时间,则将原来的 file_2.log 重命名,并添加一个新的 file_2.log 文件。
按大小滚动
除了按时间滚动日志外,Loguru 还可以按日志大小滚动:
from loguru import logger
logger.add("file_1.log", rotation="1 MB") # 滚动大日志文件
logger.debug("That's it, beautiful and simple logging!")
这样,一旦日志文件大小超过 1 MB 就会产生新的日志文件。
压缩日志
如果你不想删除原有日志文件,Loguru 还支持将日志直接压缩:
from loguru import logger
logger.add("file_Y.log", compression="zip") # 压缩日志
边栏推荐
- 【练习-10】 Unread Messages(未读消息)
- socket通讯
- [exercise-8] (UVA 246) 10-20-30== simulation
- C language must memorize code Encyclopedia
- Research Report on surgical fluid treatment industry - market status analysis and development prospect prediction
- Gartner:关于零信任网络访问最佳实践的五个建议
- 信息安全-安全编排自动化与响应 (SOAR) 技术解析
- 605. Planting flowers
- Auto. Getting started with JS
- [exercise-4] (UVA 11988) broken keyboard = = (linked list)
猜你喜欢

Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool

628. Maximum product of three numbers

Information security - Analysis of security orchestration automation and response (soar) technology

1323. Maximum number of 6 and 9

渗透测试 ( 3 ) --- Metasploit Framework ( MSF )

【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)

Luogu P1102 A-B number pair (dichotomy, map, double pointer)

Gartner:关于零信任网络访问最佳实践的五个建议

Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack

2027. Minimum number of operations to convert strings
随机推荐
【高老师UML软件建模基础】20级云班课习题答案合集
【练习-10】 Unread Messages(未读消息)
Opencv learning log 18 Canny operator
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
【练习-3】(Uva 442)Matrix Chain Multiplication(矩阵链乘)
Information security - threat detection - detailed design of NAT log access threat detection platform
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
Data storage in memory & loading into memory to make the program run
Alice and Bob (2021 Niuke summer multi school training camp 1)
MySQL授予用户指定内容的操作权限
Common configuration files of SSM framework
SSM框架常用配置文件
【练习-7】(Uva 10976)Fractions Again?!(分数拆分)
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
Openwrt build Hello ipk
[exercise-3] (UVA 442) matrix chain multiplication
[exercise-2] (UVA 712) s-trees
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
China's peripheral catheter market trend report, technological innovation and market forecast