Python 日志模块logging

  • 原创
  • Madman
  • /
  • /
  • 0
  • 9700 次阅读

Synopsis: Python程序中可以使用内置的logging模块,方便的记录运行日志,将日志信息进行分类存储,当程序出现BUG时,可以通过分析日志来定位问题所在

1. 基本概念

import logging


logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')
logging.error('This is error message')
logging.critical('This is critical message')

输出结果:

2018-06-27 15:21:37,941 - root - WARNING - This is warning message
2018-06-27 15:21:37,941 - root - ERROR - This is error message
2018-06-27 15:21:37,942 - root - CRITICAL - This is critical message

其中,2018-06-27 15:21:37,941对应%(asctime)s,表示当前时间;root对应%(name)s,表示logger实例的名称,默认名称是root;WARNING对应%(levelname)s,表示日志级别(可以看到默认是WARNING,它以下的INFO和DEBUG都不显示);This is warning message对应%(message)s,表示用户要输出的日志内容

1.1 日志级别

级别Level 对应数值
logging.NOTSET 0
logging.DEBUG 10
logging.INFO 20
logging.WARNING (默认级别) 30
logging.ERROR 40
logging.CRITICAL 50
In [1]: import logging

In [2]: logging.NOTSET
Out[2]: 0

In [3]: logging.DEBUG
Out[3]: 10

In [4]: logging.INFO
Out[4]: 20

In [5]: logging.WARNING
Out[5]: 30

In [6]: logging.ERROR
Out[6]: 40

In [7]: logging.CRITICAL
Out[7]: 50

如本文开头示例,默认日志级别是WARNING,它以下的INFODEBUG都不显示。

假设设置了日志级别为INFO,则会输出级别大于或等于INOF的日志,而小于它的如DEBUG则不会输出:

import logging


logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')
logging.error('This is error message')
logging.critical('This is critical message')

输出结果:

2018-06-27 15:39:15,582 - root - INFO - This is info message
2018-06-27 15:39:15,583 - root - WARNING - This is warning message
2018-06-27 15:39:15,
                                
                            
  • Miao jingjing
  • jackleek
  • raojingpeng
  • __豆约翰__
  • Immediate Action Required Claim Your Bonus cv039824.tw1.ru 51
  • Make Cash from Irregular Chores lyuofutykc.temp.swtest.ru Zn
  • Quantities of cash rewards are finite cc032601.tw1.ru hU
  • Download our app for instant cash cv039824.tw1.ru iD
  • A monetary gift for you personally cv039824.tw1.ru FZ
  • Sign up to receive your prize lyuofutykc.temp.swtest.ru eA
  • You have 12 hours to get this offer cv039824.tw1.ru 9f
  • Reply and get an instant reward lyuofutykc.temp.swtest.ru KP
  • A Financial Reward Can Be Won Today lyuofutykc.temp.swtest.ru Jy
  • Register to Obtain a Monetary Gift cc032601.tw1.ru LM
  • Benefit from Aid and Additional Capital cc032601.tw1.ru uV
  • There's a cash bonus with your name on it cc032601.tw1.ru tI
  • Earn cash just for signing up cc032601.tw1.ru El
  • Notification A sudden influx of cash cv039824.tw1.ru fQ
  • Access Your Exclusive Cash Bonus cc032601.tw1.ru cD
  • Spin for your chance to cash out lyuofutykc.temp.swtest.ru 7E
  • Join now and receive bonus cash cc032601.tw1.ru WM
  • Your membership unlocks these funds lyuofutykc.temp.swtest.ru 42
  • Get a Surge of Cash This Very Day cx180165.tw1.ru AM
  • Receive Your Secret Bonus fdjhgkhjkg.temp.swtest.ru 9x
未经允许不得转载: LIFE & SHARE - 王颜公子 » Python 日志模块logging

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列