Python 第三方日志框架loguru使用
解決中文亂碼問(wèn)題
項(xiàng)目地址 github: https://github.com/Delgan/loguru
文檔:https://loguru.readthedocs.io/en/stable/index.html
安裝
pip install loguru
1、輸出日志
from loguru import logger logger.debug("這是一條debug日志")
終端執(zhí)行后出現(xiàn)帶顏色的日志,挺酷的
2、輸出到文件
from loguru import logger logger.add("file_{time}.log") logger.debug("這是一條debug日志") logger.info("這是一條info日志")
目錄下多出一個(gè)日志文件 :file_2019-03-14_19-53-25_661314.log
3、日志規(guī)則
設(shè)置日志格式,過(guò)濾器,日志級(jí)別
from loguru import logger logger.add("file.log", format="{time} {level} {message}", filter="", level="INFO") logger.debug("這是一條debug日志") logger.info("這是一條info日志")
輸出
2019-03-14T20:01:25.392454+0800 INFO 這是一條info日志
4、日志文件
文件管理方式
logger.add("file_1.log", rotation="500 MB") # 文件過(guò)大就會(huì)重新生成一個(gè)文件 logger.add("file_2.log", rotation="12:00") # 每天12點(diǎn)創(chuàng)建新文件 logger.add("file_3.log", rotation="1 week") # 文件時(shí)間過(guò)長(zhǎng)就會(huì)創(chuàng)建新文件 logger.add("file_X.log", retention="10 days") # 一段時(shí)間后會(huì)清空 logger.add("file_Y.log", compression="zip") # 保存zip格式
5、其他參數(shù)
logger.add("somefile.log", enqueue=True) # 異步寫入 logger.add("somefile.log", serialize=True) # 序列化為json
6、時(shí)間格式化
logger.add("file.log", format="{time:YYYY-MM-DD at HH:mm:ss} | {level} | {message}")
配合notifiers模塊
github: https://github.com/notifiers/notifiers
文檔:https://notifiers.readthedocs.io/en/latest/
7、在工程中創(chuàng)建多個(gè)文件處理器對(duì)象并解決中文亂碼問(wèn)題
# coding=utf-8 import os import sys from loguru import logger BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) log_file_path = os.path.join(BASE_DIR, 'Log/my.log') err_log_file_path = os.path.join(BASE_DIR, 'Log/err.log') logger.add(sys.stderr, format="{time} {level} {message}", filter="my_module", level="INFO") # logger.add(s) logger.add(log_file_path, rotation="500 MB", encoding='utf-8') # Automatically rotate too big file logger.add(err_log_file_path, rotation="500 MB", encoding='utf-8', level='ERROR') # Automatically rotate too big file logger.debug("That's it, beautiful and simple logging!") logger.debug("中文日志可以不") logger.error("嚴(yán)重錯(cuò)誤")
以上就是Python 第三方日志框架loguru使用的詳細(xì)內(nèi)容,更多關(guān)于Python 日志框架loguru的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python實(shí)現(xiàn)簡(jiǎn)易凱撒密碼的示例代碼
密碼的使用最早可以追溯到古羅馬時(shí)期,《高盧戰(zhàn)記》有描述愷撒曾經(jīng)使用密碼來(lái)傳遞信息,即所謂的“愷撒密碼”。本文將利用Python實(shí)現(xiàn)簡(jiǎn)易的凱撒密碼,感興趣的可以了解一下2022-09-09python+mysql實(shí)現(xiàn)簡(jiǎn)單的web程序
上篇文章我們介紹了簡(jiǎn)單的Python web程序,實(shí)現(xiàn)hello world,本文我們來(lái)結(jié)合一下mysql,實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的簡(jiǎn)單操作,希望對(duì)大家有所幫助2014-09-09python爬蟲MeterSphere平臺(tái)執(zhí)行報(bào)告使用實(shí)戰(zhàn)
這篇文章主要為大家介紹了python爬蟲MeterSphere平臺(tái)執(zhí)行報(bào)告使用實(shí)戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12python的import?機(jī)制是怎么實(shí)現(xiàn)的
這篇文章主要介紹了python的import?機(jī)制是怎么實(shí)現(xiàn)的,import有Python運(yùn)行時(shí)的全局模塊池的維護(hù)和搜索、解析與搜索模塊路徑的樹(shù)狀結(jié)構(gòu)等作用,下文具體相關(guān)介紹需要的小伙伴可以參考一下2022-05-05解決python錯(cuò)誤提示:TypeError: expected string or&nb
這篇文章主要介紹了解決python錯(cuò)誤提示:TypeError: expected string or bytes-lik問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Python并發(fā)編程之未來(lái)模塊Futures
這篇文章主要為大家介紹了Python的未來(lái),python并發(fā)編程之未來(lái)模塊Futures的詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05