Python tabulate結合loguru打印出美觀方便的日志記錄
在開發(fā)過程中經常碰到在本地環(huán)境無法完成聯調測試的情況,必須到統(tǒng)一的聯機環(huán)境對接其他系統(tǒng)測試。往往是出現了BUG難以查找數據記錄及時定位到錯誤出現的位置。
面對這種情況可能情況可能是一個簡單的BUG導致的,但是定位問題往往就需要很長的時間。在python編程中推薦非標準庫tabulate,它可以將程序運行過程中產生的數據記錄格式化的打印出來很方便我們定位問題。
通過結合簡單的日志非標準庫loguru可以很快的打印出又美觀又實用的日志記錄,loguru非標準庫其實在一些文章的源碼示例中我們已經在使用了。
安裝過程還是通過pip的方式直接安裝,由于loguru、tabulate都是python的非標準庫,因此,沒有安裝的話需要安裝一下。默認我們都使用的清華大學的python鏡像站,大家可以選擇其他的鏡像站都可以。
pip install loguru -i https://pypi.tuna.tsinghua.edu.cn/simple/ pip install tabulate - i https://pypi.tuna.tsinghua.edu.cn/simple/
做好準備工作以后將loguru、tabulate模塊都導入進來就OK了,沒有其他復雜的操作!
# It's a shortcut to create a logger with the default configuration. from loguru import logger # It's importing the function `tabulate` from the module `tabulate`. from tabulate import tabulate
關于非標準庫tabulate,它的打印模式其實有很多,我們平常使用到的可能就是幾種比較常見的,下面將tabulate所有的打印模式全部列舉出來,有需要的大佬可以參考。
''' "plain" "simple" "github" "grid" "fancy_grid" "pipe" "orgtbl" "jira" "presto" "psql" "rst" "mediawiki" "moinmoin" "youtrack" "html" "latex" "latex_raw" "latex_booktabs" "textile" '''
我們選擇其中的一種'grid'模式來看看效果如何,因為這種模式打印的數據記錄也是比較常見的。下面創(chuàng)建一個函數tab_grid_log打印一段數組形式的數據記錄。
def tab_grid_log():
"""
> This function takes a list of lists and returns a list of lists with the log of each element
"""
# It's defining the header of the table.
header = [u'姓名', u'年齡', u'班級', u'成績', u'表現']
# It's defining a list of lists.
table = [('Python', 20, 1710, 98, 5.0), ('Java', 22, 1810, 98, 4.9)]
# It's printing the table with the `grid` format.
logger.info(tabulate(table, headers=header, tablefmt='grid'))
tab_grid_log()
# 2022-09-17 18:33:00.472 | INFO | __main__:tab_grid_log:66 - +--------+--------+--------+--------+--------+
# | 姓名 | 年齡 | 班級 | 成績 | 表現 |
# +========+========+========+========+========+
# | Python | 20 | 1710 | 98 | 5 |
# +--------+--------+--------+--------+--------+
# | Java | 22 | 1810 | 98 | 4.9 |
# +--------+--------+--------+--------+--------+
#
# Process finished with exit code 0
使用grid模式的打印的數據記錄就是宮格形式很便于查找日志中的數據記錄,也是我們經常在日志記錄中使用的一種打印方法。
接下來我們隨便選擇一種模式再次進行打印,這里就選擇html模式來看看效果吧,這種模式之前沒有使用過我很好奇它會打印出什么樣的效果。
def tab_html_log():
"""
> This function takes a log file and returns a html table of the log file
"""
# It's defining the header of the table.
header = [u'姓名', u'年齡', u'班級', u'成績', u'表現']
# It's defining a list of lists.
table = [('Python', 20, 1710, 98, 5.0), ('Java', 22, 1810, 98, 4.9)]
# It's printing the table with the `html` format.
logger.info(tabulate(table, headers=header, tablefmt='html'))
tab_html_log()
# 2022-09-17 18:37:50.383 | INFO | __main__:tab_html_log:87 - <table>
# <thead>
# <tr><th>姓名 </th><th style="text-align: right;"> 年齡</th><th style="text-align: right;"> 班級</th><th style="text-align: right;"> 成績</th><th style="text-align: right;"> 表現</th></tr>
# </thead>
# <tbody>
# <tr><td>Python</td><td style="text-align: right;"> 20</td><td style="text-align: right;"> 1710</td><td style="text-align: right;"> 98</td><td style="text-align: right;"> 5 </td></tr>
# <tr><td>Java </td><td style="text-align: right;"> 22</td><td style="text-align: right;"> 1810</td><td style="text-align: right;"> 98</td><td style="text-align: right;"> 4.9</td></tr>
# </tbody>
# </table>
從打印結果可以看出來了,使用html模式的打印時實際上是生成html的源碼,這還是很智能的包括style的樣式屬性也填充了。
到此這篇關于Python tabulate結合loguru打印出美觀方便的日志記錄的文章就介紹到這了,更多相關Python tabulate loguru打印日志記錄內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python3連接mysql獲取ansible動態(tài)inventory腳本
Ansible Inventory 是包含靜態(tài) Inventory 和動態(tài) Inventory 兩部分的,靜態(tài) Inventory 指的是在文件中指定的主機和組,動態(tài) Inventory 指通過外部腳本獲取主機列表。這篇文章主要介紹了python3連接mysql獲取ansible動態(tài)inventory腳本,需要的朋友可以參考下2020-01-01
python pandas dataframe如何獲取除了指定列以外的所有列
這篇文章主要介紹了python pandas dataframe如何獲取除了指定列以外的所有列問題,具有很好的參考價值,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09

