当前位置:网站首页>Flask框架配置loguru日志庫
Flask框架配置loguru日志庫
2022-07-06 16:09: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") # 壓縮日志
边栏推荐
- Opencv learning log 15 count the number of solder joints and output
- New to redis
- [teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
- Opencv learning log 33 Gaussian mean filtering
- Common configuration files of SSM framework
- MySQL grants the user the operation permission of the specified content
- Candy delivery (Mathematics)
- 信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
- Programmers, what are your skills in code writing?
- Analysis of protobuf format of real-time barrage and historical barrage at station B
猜你喜欢

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

Penetration test (8) -- official document of burp Suite Pro

Candy delivery (Mathematics)

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

Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools

树莓派4B安装opencv3.4.0

渗透测试 ( 1 ) --- 必备 工具、导航

信息安全-威胁检测引擎-常见规则引擎底座性能比较
![[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class](/img/3b/dc43564a36f82e73826b08f39c935e.png)
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class

Vs2019 initial use
随机推荐
[exercise-2] (UVA 712) s-trees
Nodejs+vue online fresh flower shop sales information system express+mysql
Ball Dropping
2078. Two houses with different colors and the farthest distance
Opencv learning log 30 -- histogram equalization
C language is the watershed between low-level and high-level
1605. Sum the feasible matrix for a given row and column
New to redis
Opencv learning log 24 -- Hough transform 2 (maximum interval and minimum length can be limited)
Opencv learning log 15 count the number of solder joints and output
921. Minimum additions to make parentheses valid
信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
[exercise-5] (UVA 839) not so mobile (balance)
D - function (HDU - 6546) girls' competition
Nodejs+vue网上鲜花店销售信息系统express+mysql
Information security - threat detection - detailed design of NAT log access threat detection platform
Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
628. Maximum product of three numbers
TCP's three handshakes and four waves
7-1 understand everything (20 points)