当前位置:网站首页>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") # 壓縮日志
边栏推荐
- b站 實時彈幕和曆史彈幕 Protobuf 格式解析
- Interesting drink
- Information security - threat detection - detailed design of NAT log access threat detection platform
- 信息安全-安全专业名称|CVE|RCE|POC|VUL|0DAY
- Research Report of exterior wall insulation system (ewis) industry - market status analysis and development prospect prediction
- 【练习-10】 Unread Messages(未读消息)
- Socket communication
- 信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
- 【练习-3】(Uva 442)Matrix Chain Multiplication(矩阵链乘)
- b站 实时弹幕和历史弹幕 Protobuf 格式解析
猜你喜欢
frida hook so层、protobuf 数据解析

1323. Maximum number of 6 and 9

Penetration test (7) -- vulnerability scanning tool Nessus

2078. Two houses with different colors and the farthest distance

Nodejs+vue online fresh flower shop sales information system express+mysql

Write web games in C language

C language is the watershed between low-level and high-level

C language learning notes

渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞

信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
随机推荐
1689. Ten - the minimum number of binary numbers
【练习-10】 Unread Messages(未读消息)
渗透测试 ( 1 ) --- 必备 工具、导航
[exercise-9] Zombie's Treasury test
[exercise-6] (PTA) divide and conquer
Socket communication
滲透測試 ( 1 ) --- 必備 工具、導航
【练习-6】(Uva 725)Division(除法)== 暴力
HDU - 6024 building shops (girls' competition)
Opencv learning log 19 skin grinding
Research Report of exterior wall insulation system (ewis) industry - market status analysis and development prospect prediction
Auto. Getting started with JS
Auto.js入门
最全编程语言在线 API 文档
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
605. Planting flowers
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
Openwrt source code generation image
Interval sum ----- discretization
Openwrt build Hello ipk