为了账号安全,请及时绑定邮箱和手机立即绑定

tabulate结合loguru打印出美观又方便查找的日志记录!

标签:
Python

在开发过程中经常碰到在本地环境无法完成联调测试的情况,必须到统一的联机环境对接其他系统测试。往往是出现了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’模式来看看效果如何,因为这种模式打印的数据记录也是比较常见的。下面创建一个函数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的样式属性也填充了。

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消