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

打印 上一主题 下一主题

主题 891|帖子 891|积分 2673

在开发过程中经常碰到在本地环境无法完成联调测试的情况,必须到统一的联机环境对接其他系统测试。往往是出现了BUG难以查找数据记录及时定位到错误出现的位置。
【阅读全文】
面对这种情况可能情况可能是一个简单的BUG导致的,但是定位问题往往就需要很长的时间。在python编程中推荐非标准库tabulate,它可以将程序运行过程中产生的数据记录格式化的打印出来很方便我们定位问题。
通过结合简单的日志非标准库loguru可以很快的打印出又美观又实用的日志记录,loguru非标准库其实在一些文章的源码示例中我们已经在使用了。
安装过程还是通过pip的方式直接安装,由于loguru、tabulate都是python的非标准库,因此,没有安装的话需要安装一下。默认我们都使用的清华大学的python镜像站,大家可以选择其他的镜像站都可以。
  1. pip install loguru -i https://pypi.tuna.tsinghua.edu.cn/simple/
  2. pip install tabulate - i https://pypi.tuna.tsinghua.edu.cn/simple/
复制代码
做好准备工作以后将loguru、tabulate模块都导入进来就OK了,没有其他复杂的操作!
  1. # It's a shortcut to create a logger with the default configuration.
  2. from loguru import logger
  3. # It's importing the function `tabulate` from the module `tabulate`.
  4. from tabulate import tabulate
复制代码
关于非标准库tabulate,它的打印模式其实有很多,我们平常使用到的可能就是几种比较常见的,下面将tabulate所有的打印模式全部列举出来,有需要的大佬可以参考。
  1. '''
  2. "plain"
  3. "simple"
  4. "github"
  5. "grid"
  6. "fancy_grid"
  7. "pipe"
  8. "orgtbl"
  9. "jira"
  10. "presto"
  11. "psql"
  12. "rst"
  13. "mediawiki"
  14. "moinmoin"
  15. "youtrack"
  16. "html"
  17. "latex"
  18. "latex_raw"
  19. "latex_booktabs"
  20. "textile"
  21. '''
复制代码
我们选择其中的一种'grid'模式来看看效果如何,因为这种模式打印的数据记录也是比较常见的。下面创建一个函数tab_grid_log打印一段数组形式的数据记录。
  1. def tab_grid_log():
  2.     """
  3.     > This function takes a list of lists and returns a list of lists with the log of each element
  4.     """
  5.     # It's defining the header of the table.
  6.     header = [u'姓名', u'年龄', u'班级', u'成绩', u'表现']
  7.     # It's defining a list of lists.
  8.     table = [('Python', 20, 1710, 98, 5.0), ('Java', 22, 1810, 98, 4.9)]
  9.     # It's printing the table with the `grid` format.
  10.     logger.info(tabulate(table, headers=header, tablefmt='grid'))
  11. tab_grid_log()
  12. # 2022-09-17 18:33:00.472 | INFO     | __main__:tab_grid_log:66 - +--------+--------+--------+--------+--------+
  13. # | 姓名   |   年龄 |   班级 |   成绩 |   表现 |
  14. # +========+========+========+========+========+
  15. # | Python |     20 |   1710 |     98 |    5   |
  16. # +--------+--------+--------+--------+--------+
  17. # | Java   |     22 |   1810 |     98 |    4.9 |
  18. # +--------+--------+--------+--------+--------+
  19. #
  20. # Process finished with exit code 0
复制代码
使用grid模式的打印的数据记录就是宫格形式很便于查找日志中的数据记录,也是我们经常在日志记录中使用的一种打印方法。
接下来我们随便选择一种模式再次进行打印,这里就选择html模式来看看效果吧,这种模式之前没有使用过我很好奇它会打印出什么样的效果。
  1. def tab_html_log():
  2.     """
  3.     > This function takes a log file and returns a html table of the log file
  4.     """
  5.     # It's defining the header of the table.
  6.     header = [u'姓名', u'年龄', u'班级', u'成绩', u'表现']
  7.     # It's defining a list of lists.
  8.     table = [('Python', 20, 1710, 98, 5.0), ('Java', 22, 1810, 98, 4.9)]
  9.     # It's printing the table with the `html` format.
  10.     logger.info(tabulate(table, headers=header, tablefmt='html'))
  11. tab_html_log()
  12. # 2022-09-17 18:37:50.383 | INFO     | __main__:tab_html_log:87 - <table>
  13. # <thead>
  14. # <tr><th>姓名  </th><th >  年龄</th><th >  班级</th><th >  成绩</th><th >  表现</th></tr>
  15. # </thead>
  16. # <tbody>
  17. # <tr><td>Python</td><td >    20</td><td >  1710</td><td >    98</td><td >   5  </td></tr>
  18. # <tr><td>Java  </td><td >    22</td><td >  1810</td><td >    98</td><td >   4.9</td></tr>
  19. # </tbody>
  20. # </table>
复制代码
从打印结果可以看出来了,使用html模式的打印时实际上是生成html的源码,这还是很智能的包括style的样式属性也填充了。
【精彩推荐】
问题记录:为什们有的python底层代码块函数却只有一个pass?
我使用pangu模块做了一个文本格式化小工具!
ping命令的多种玩法,以前竟然只用它来测试网速!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

络腮胡菲菲

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表