Python中的traceback模块被用于跟踪异常返回信息,可以在logging中记录下traceback。

traceback.format_exc() 获取异常为字符串,保存到日志文件

try:
import lxml
except:
trace = traceback.format_exc()
logger.error(f'\n{trace}') 

日志文件输出:

[2018-10-26 17:20:47,698] - [__main__] - [ERROR] -
Traceback (most recent call last):
File "/home/ldy/myself/untitled/scripts/scripts.py", line 20, in <module>
import lxml
ModuleNotFoundError: No module named 'lxml'

与traceback.print_exc()的区别是,后者直接输出在控制台。

Python traceback模块简单使用的更多相关文章

  1. Python traceback 模块,追踪错误

    Python traceback 模块,追踪错误 import traceback try: your code except: traceback.print_exc()

  2. python shutil模块简单介绍

    python shutil模块简单介绍 简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作. shutil 模块方法: copy(src, ...

  3. python timeit模块简单用法

    timeit模块提供了一种简便的方法来为Python中的小块代码进行计时. 模块调用函数,stmp为要测试的函数,setup为测试环境,number为运行次数 timeit.timeit(stmt=) ...

  4. python paramiko 模块简单介绍

    背景,公司的很多服务包括数据库访问都需要通过跳板机访问,为日常工作及使用带来了麻烦,特别数python直接操作数据更是麻烦了,所以一直想实现python 通过跳板机访问数据库的操作. 首先了解到了 p ...

  5. python numpy 模块简单介绍

    用python自带的list去处理数组效率很低, numpy就诞生了, 它提供了ndarry对象,N-dimensional object, 是存储单一数据类型的多维数组,即所有的元素都是同一种类型. ...

  6. Python traceback 模块, 打印异常信息

    Python感觉是模仿Java, 到处都需要加try..catch.... 这里记录一下用法,方便后续使用. # -*- coding:utf-8 -*- import os import loggi ...

  7. python argpase模块简单使用

    python2.7 手册地址:https://docs.python.org/2/howto/argparse.html#id1 实现效果:脚本程序可以带参数 python arg.py -h 一.位 ...

  8. python pandas模块简单使用(读取excel为例)

    第一步:模块安装 pip install pandas 第二步:使用(单个工作表为例) 说明:如果有多个工作表,那么只要指定sheetname=索引,(第一个工作表为0,第二个工作表为1,以此类推) ...

  9. Python日志模块简单使用

    def loginfo(info): # create logger logger = logging.getLogger('[cpu and mem]**********') # Set defau ...

随机推荐

  1. [Array]167. Two Sum II - Input array is sorted

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  2. 【python之路32】python异常处理

    一.捕获异常 1.try  except #!usr/bin/env python # -*- coding:utf-8 -*- num = input("请输入一个数字:") t ...

  3. Django项目:CRM(客户关系管理系统)--27--19PerfectCRM实现King_admin数据修改

    登陆密码设置参考 http://www.cnblogs.com/ujq3/p/8553784.html {#table_data_list.html#} {## ————————08PerfectCR ...

  4. tyvj 1423 GF和猫咪的玩具

    传送门 解题思路 题目比较水,floyd求出最短路取个最小值即可.结果joyoi时限写错了..好像只有0ms才能过??突然发现加了快读就T不加就A,数据在10000以下的还是scanf快啊. 代码 # ...

  5. php静态变量问题

    <?php$a=0; function test(){ static $a=0; $a+=1; echo $a; }test(); test(); ?>1.static是与销毁时间有关,与 ...

  6. myql 配置项

    提高数据插入速度方法 bulk_insert_buffer_size 默认:8M (8*1024*1024) 参考网址:https://stackoverflow.com/questions/2030 ...

  7. 全栈数据工程师养成攻略:Python 基本语法

    全栈数据工程师养成攻略:Python 基本语法 Python简单易学,但又博大精深.许多人号称精通Python,却不会写Pythonic的代码,对很多常用包的使用也并不熟悉.学海无涯,我们先来了解一些 ...

  8. What every computer science major should know 每一个计算机科学专业的毕业生都应该都知道的

    Given the expansive growth in the field, it's become challenging to discern what belongs in a modern ...

  9. mysql_example

    package main import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysq ...

  10. mysql查询某个字段并修改

    比如我存储的数据,有的是 山东,有的是山东省 我想统一改为山东省 UPDATE t_security SET province = REPLACE( province, '山东', '山东省' ) W ...