Python tabulate结合loguru打印出美观方便的日志记录

作者:Python 时间:2021-02-23 02:39:45 

在开发过程中经常碰到在本地环境无法完成联调测试的情况,必须到统一的联机环境对接其他系统测试。往往是出现了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的样式属性也填充了。

来源:https://www.cnblogs.com/lwsbc/p/16773668.html

标签:Python,tabulate,loguru,打印,日志
0
投稿

猜你喜欢

  • 在pytorch中对非叶节点的变量计算梯度实例

    2021-08-26 10:13:53
  • 浅析DW4中的站点管理

    2007-02-03 11:40:00
  • python 调用有道api接口的方法

    2021-11-18 18:51:16
  • Python列表的定义及使用

    2023-08-02 03:38:32
  • 解决更改AUTH_USER_MODEL后出现的问题

    2023-06-22 08:14:10
  • Elasticsearch属性单词常用解析说明

    2023-06-12 14:47:36
  • SQL Data Services将成为云中完整的数据库

    2009-03-25 12:28:00
  • 服务器端控件是如何操作的?

    2009-11-01 15:22:00
  • Window10下python3.7 安装与卸载教程图解

    2021-02-15 16:23:54
  • 基于Python实现二维图像双线性插值

    2023-08-13 07:46:45
  • 详解Python中高阶函数(map,filter,reduce,sorted)的使用

    2023-10-24 15:39:09
  • 使用Python实现二分法查找的示例

    2022-02-08 13:52:53
  • django 通过ajax完成邮箱用户注册、激活账号的方法

    2022-02-19 05:59:57
  • Python标准库inspect的具体使用方法

    2023-05-30 08:00:37
  • 在Python中使用SimpleParse模块进行解析的教程

    2021-04-11 12:17:53
  • python命令行参数argparse模块基本用法详解

    2023-07-31 03:14:21
  • 完美解决Pycharm中matplotlib画图中文乱码问题

    2021-11-01 00:25:35
  • python web框架 django wsgi原理解析

    2021-11-21 02:44:59
  • python对站点数据做EOF且做插值绘制填色图

    2023-03-05 03:30:56
  • 基于Google的Python编码规范标准

    2023-02-21 12:40:38
  • asp之家 网络编程 m.aspxhome.com