当前位置:网站首页>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") # 壓縮日志
边栏推荐
- Flink 使用之 CEP
- 【练习-10】 Unread Messages(未读消息)
- Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
- Radar equipment (greedy)
- D - function (HDU - 6546) girls' competition
- Luogu P1102 A-B number pair (dichotomy, map, double pointer)
- Shell Scripting
- 渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
- Openwrt source code generation image
- 1005. Maximized array sum after K negations
猜你喜欢
X-Forwarded-For详解、如何获取到客户端IP
807. Maintain the urban skyline
C language must memorize code Encyclopedia
信息安全-威胁检测引擎-常见规则引擎底座性能比较
Sword finger offer II 019 Delete at most one character to get a palindrome
1323. Maximum number of 6 and 9
Gartner: five suggestions on best practices for zero trust network access
Information security - threat detection - detailed design of NAT log access threat detection platform
Quick to typescript Guide
Ball Dropping
随机推荐
1605. Sum the feasible matrix for a given row and column
Sword finger offer II 019 Delete at most one character to get a palindrome
Find 3-friendly Integers
Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
【练习-2】(Uva 712) S-Trees (S树)
X-Forwarded-For详解、如何获取到客户端IP
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
X-forwarded-for details, how to get the client IP
1013. Divide the array into three parts equal to and
628. Maximum product of three numbers
【练习4-1】Cake Distribution(分配蛋糕)
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
Quick to typescript Guide
1903. Maximum odd number in string
树莓派4B安装opencv3.4.0
Opencv learning log 26 -- detect circular holes and mark them
Luogu P1102 A-B number pair (dichotomy, map, double pointer)
Opencv learning log 30 -- histogram equalization
C language learning notes
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档