日志报错要去修,要不然是隐患,总有一天会爆炸

增加日志是排错的好方法,不要不舍得加日志,比如怕代码变难看,怕日志输出太多。

python logging

exc_info

sys.exc_info()

This function returns a tuple of three values that give information about the exception that is currently being handled. The information returned is specific both to the current thread and to the current stack frame. If the current stack frame is not handling an exception, the information is taken from the calling stack frame, or its caller, and so on until a stack frame is found that is handling an exception. Here, “handling an exception” is defined as “executing an except clause.” For any stack frame, only information about the exception being currently handled is accessible.

If no exception is being handled anywhere on the stack, a tuple containing three None values is returned. Otherwise, the values returned are (type, value, traceback). Their meaning is: type gets the type of the exception being handled (a subclass of BaseException); value gets the exception instance (an instance of the exception type); traceback gets a traceback object (see the Reference Manual) which encapsulates the call stack at the point where the exception originally occurred.

stack_info

traceback.print_stack(f=None, limit=None, file=None)

Print up to limit stack trace entries (starting from the invocation point) if limit is positive. Otherwise, print the last abs(limit) entries. If limit is omitted or None, all entries are printed. The optional f argument can be used to specify an alternate stack frame to start. The optional file argument has the same meaning as for print_tb().

Changed in version 3.5: Added negative limit support.

两个参数确实增加了日志信息:

>>> import logging
>>> try:
... assert False
... except Exception as e:
... print('e: %s' % e)
... logging.error(e, exc_info=True, stack_info=True)
...
e:
ERROR:root:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
AssertionError
Stack (most recent call last):
File "<stdin>", line 5, in <module>
>>> try:
... assert False
... except Exception as e:
... logging.error(e)
...
ERROR:root:

  

是否 log 都要加上以上两个参数,如果都要,python 为什么不将这两个参数变为默认参数?

这两个参数可否在 dict config 中配置?

python LogRecord attributes 存在

但是文档中写不需要 format:

exc_info

You shouldn’t need to format this yourself.

Exception tuple (à la sys.exc_info) or, if no exception has occurred, None. 

stack_info

You shouldn’t need to format this yourself.

Stack frame information (where available) from the bottom of the stack in the current thread, up to and including the stack frame of the logging call which resulted in the creation of this record.

看了sentry的Django文档,没有找到能够配置 exc_info 与 stack_info 的参数。文档中都是显式调用

exc_info and extra={'stack': True}

  

python 自带的 logging 我设置了

%(exc_info)s: %(stack_info)s

,但好像没有生效。

还有一个需求,调用函数的时候出错,日志能否自动记录下调用函数的参数?

说完了格式,说下需要打日志的地方。

1. 异常分支或错误处理一定要打log

2. 重大操作时一定要打log,下面打log场景会讲述

与金钱相关。

追踪一笔订单的整个生命周期

从下单最后完成支付,发货的整个周期都需要能够通过日志追踪。

参考

软件打log的一些心得

打日志--以python为例的更多相关文章

  1. 日志服务Python消费组实战(三):实时跨域监测多日志库数据

    解决问题 使用日志服务进行数据处理与传递的过程中,你是否遇到如下监测场景不能很好的解决: 特定数据上传到日志服务中需要检查数据内的异常情况,而没有现成监控工具? 需要检索数据里面的关键字,但数据没有建 ...

  2. 日志服务Python消费组实战(二):实时分发数据

    场景目标 使用日志服务的Web-tracking.logtail(文件极简).syslog等收集上来的日志经常存在各种各样的格式,我们需要针对特定的日志(例如topic)进行一定的分发到特定的logs ...

  3. 以Python为例的Async / Await的编程基础

    来源:Redislabs 作者:Loris Cro 翻译:Kevin (公众号:中间件小哥) 近年来,许多编程语言都在努力改进它们的并发原语.Go 语言有 goroutines,Ruby 有 fibe ...

  4. 【转】以Python为例的Async / Await的编程基础

    转, 原文:https://www.cnblogs.com/middleware/p/11996731.html 以Python为例的Async / Await的编程基础 -------------- ...

  5. 以python为例讲解闭包机制

    以python为例讲解闭包机制 缘起 在学习JS的过程中,总是无可避免的接触到闭包机制,尤其是接触到react后,其函数式的编程思想更是将闭包发扬光大,作为函数式编程的重要语法结构,python自然也 ...

  6. Python 100例(上)

    如果你已经把基础看完,可以尝试一下看看以下例子了,如果不会做也不要紧,你要尝试手动把所有的代码都敲一边.别嫌麻烦,因为都是从麻烦到简单的. 实例1: 题目:有1.2.3.4个数字,能组成多少个相互不同 ...

  7. 软件目录结构规范(以python为例)

    为什么要设计好目录结构   "设计项目目录结构",就和"代码编码风格"一样,属于个人风格问题.对于这种风格上的规范,一直都存在两种态度: 一类同学认为,这种个人 ...

  8. [Python] Python 100例

    题目1:有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. #程序源 ...

  9. python单例(重点)

    单例 目标 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式 设计模式 是 前人工作的总结和提炼,通常,被人们广泛流传的设计模式都是针对 某一特定问题的成熟的 ...

随机推荐

  1. grep和map计算两个集合交集、并集、补集

    #!/usr/bin/perl use strict; ######################################## 用grep 和map 获取两个列表的交集并集.补集###### ...

  2. msbuild,Build failed with Error MSB3073 exited with code 1

    1. 接手以前的老项目,因为项目比较大,所以用Developer Command Prompt 的msbuild命令编译比较快一些,常用命令如下 devenv /?             帮助 ms ...

  3. NIO之DatagramChannel

    Java NIO中的DatagramChannel是一个能收发UDP包的通道.操作步骤: 1)打开 DatagramChannel 2)接收/发送数据 同样它也支持NIO的非阻塞模式操作,例如: pu ...

  4. NIO之阻塞IO与非阻塞IO(包含Selector使用)

    阻塞IO 传统的 IO 流都是阻塞式的. 也就是说,当一个线程调用 read() 或 write()时,该线程被阻塞,直到有一些数据被读取或写入,该线程在此期间不能执行其他任务. 因此,在完成网络通信 ...

  5. Atitit. C#.net clr 2.0  4.0新特性

    Atitit. C#.net clr 2.0  4.0新特性 1. CLR内部结构1 2. CLR 版本发展史3 3. CLR 2.0 3 4. CLR 4 新特性 概览4 4.1.1.  托管与本地 ...

  6. C# AppDomain 类

    /*** AppDomain 表示应用程序域,它是一个应用程序在其中执行的独立环境.无法继承此类. 应用程序域(由 AppDomain 对象表示)为执行托管代码提供隔离.卸载和安全边界. 使用应用程序 ...

  7. iOS10开发需要注意的一些问题(转)

    兼容iOS 10 资料整理笔记 2016-09-17 判若两人丶 CocoaChina ▲点击上方“CocoaChina”关注即可免费学习iOS开发 原文链接:http://www.jianshu.c ...

  8. IOS设计模式的六大设计原则之迪米特法则(LOD,Law Of Demeter)

    定义 狭义的迪米特法则定义:也叫最少知识原则(LKP,Least Knowledge Principle).如果两个类不必彼此直接通信,那么这两个类就不应当发生直接的相互作用.如果其中的一个类需要调用 ...

  9. Java HashMap中在resize()时候的rehash,即再哈希法的理解

    HashMap的扩容机制---resize() 虽然在hashmap的原理里面有这段,但是这个单独拿出来讲rehash或者resize()也是极好的. 什么时候扩容:当向容器添加元素的时候,会判断当前 ...

  10. Python - except不指定异常类别(转)

    From:How to properly ignore Exceptions? try: doSomething() except: pass or try: doSomething() except ...