Python 第三方日志框架loguru使用
項(xiàng)目地址 github: https://github.com/Delgan/loguru文檔:https://loguru.readthedocs.io/en/stable/index.html
安裝pip install loguru
1、輸出日志
from loguru import loggerlogger.debug('這是一條debug日志')
終端執(zhí)行后出現(xiàn)帶顏色的日志,挺酷的

2、輸出到文件
from loguru import loggerlogger.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 loggerlogger.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ì)清空l(shuí)ogger.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-8import osimport sysfrom loguru import loggerBASE_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 filelogger.add(err_log_file_path, rotation='500 MB', encoding=’utf-8’, level=’ERROR’) # Automatically rotate too big filelogger.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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享2. vue項(xiàng)目登錄成功拿到令牌跳轉(zhuǎn)失敗401無(wú)登錄信息的解決3. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)4. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能5. uniapp自定義驗(yàn)證碼輸入框并隱藏光標(biāo)6. 淺談?dòng)蓀osition屬性引申的css進(jìn)階討論7. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向8. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享9. vue前端RSA加密java后端解密的方法實(shí)現(xiàn)10. asp批量添加修改刪除操作示例代碼

網(wǎng)公網(wǎng)安備