python logging日志模块的详解

yipeiwu_com6年前Python基础

python logging日志模块的详解

日志级别

日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRITICAL。
DEBUG:详细的信息,通常只出现在诊断问题上
INFO:确认一切按预期运行
WARNING:一个迹象表明,一些意想不到的事情发生了,或表明一些问题在不久的将来(例如。磁盘空间低”)。这个软件还能按预期工作。
ERROR:更严重的问题,软件没能执行一些功能
CRITICAL:一个严重的错误,这表明程序本身可能无法继续运行
这5个等级,也分别对应5种打日志的方法: debug 、info 、warning 、error 、critical。默认的是WARNING,当在WARNING或之上时才被跟踪。

日志格式说明

logging.basicConfig函数中,可以指定日志的输出格式format,这个参数可以输出很多有用的信息,如上例所示:
%(levelno)s: 打印日志级别的数值
%(levelname)s: 打印日志级别名称
%(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
%(filename)s: 打印当前执行程序名
%(funcName)s: 打印日志的当前函数
%(lineno)d: 打印日志的当前行号
%(asctime)s: 打印日志的时间
%(thread)d: 打印线程ID
%(threadName)s: 打印线程名称
%(process)d: 打印进程ID
%(message)s: 打印日志信息
我在工作中给的常用格式在前面已经看到了。就是:
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'
这个格式可以输出日志的打印时间,是哪个模块输出的,输出的日志级别是什么,以及输入的日志内容。

日志输出

有两种方式记录跟踪,一种输出控制台,另一种是记录到文件中,如日志文件。

将日志输出到控制台

在a.py写入以下信息

import logging 

logging.basicConfig(level=logging.WARNING, 
          format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s') 
# use logging 
logging.info('this is a loggging info message') 
logging.debug('this is a loggging debug message') 
logging.warning('this is loggging a warning message') 
logging.error('this is an loggging error message') 
logging.critical('this is a loggging critical message') 
执行上面的代码将在Console中输出下面信息:
2017-03-16 16:58:11,266 - a.py[line:10] - WARNING: this is loggging a warning message
2017-03-16 16:58:11,266 - a.py[line:11] - ERROR: this is an loggging error message
2017-03-16 16:58:11,266 - a.py[line:12] - CRITICAL: this is a loggging critical message

【解析】

通过logging.basicConfig函数对日志的输出格式及方式做相关配置,上面代码设置日志的输出等级是WARNING级别,意思是WARNING级别以上的日志才会输出。另外还制定了日志输出的格式。

将日志输出到文件

在logging.basicConfig函数中设置好输出文件的文件名和写文件的模式。

import logging 

logging.basicConfig(level=logging.WARNING, 
          filename='./log/log.txt', 
          filemode='w', 
          format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s') 
# use logging 
logging.info('this is a loggging info message') 
logging.debug('this is a loggging debug message') 
logging.warning('this is loggging a warning message') 
logging.error('this is an loggging error message') 
logging.critical('this is a loggging critical message') 
运行之后,打开该文件./log/log.txt,效果如下:
2015-05-21 17:30:20,282 - log.py[line:12] - WARNING: this is loggging a warning message
2015-05-21 17:30:20,282 - log.py[line:13] - ERROR: this is an loggging error message
2015-05-21 17:30:20,282 - log.py[line:14] - CRITICAL: this is a loggging critical message

通过配置文件设置日志模式

https://docs.python.org/2/library/logging.config.html

dictconfig比fileconfig要更新

#config.conf
###############################################
[loggers]
keys=root,example01,example02

[logger_root]
level=DEBUG
handlers=hand01,hand02

[logger_example01]
handlers=hand01,hand02
qualname=example01
propagate=0

[logger_example02]
handlers=hand01,hand03
qualname=example02
propagate=0

###############################################
[handlers]
keys=hand01,hand02,hand03

[handler_hand01]
class=StreamHandler
level=INFO
formatter=form02
args=(sys.stderr,)

[handler_hand02]
class=FileHandler
level=NOTSET
formatter=form01
args=('myapp.log', 'a')

[handler_hand03]
class=handlers.RotatingFileHandler
level=INFO
formatter=form02
args=('myapp.log', 'a', 10*1024*1024, 5)

###############################################
[formatters]
keys=form01,form02
[formatter_form01]
format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
datefmt=%a, %d %b %Y %H:%M:%S
[formatter_form02]
format=%(name)-12s: %(levelname)-8s %(message)s
datefmt=

主函数

import logging
import logging.config

logging.config.fileConfig("/home/razerware/configscript/config.conf")
logger = logging.getLogger("example01")
logger2 = logging.getLogger("example02")
logger.debug('This is debug message')
logger.info('This is info message')
logger.warning('This is warning message')

logger2.debug('This is debug message')
logger2.info('This is info message')
logger2.warning('This is warning message')

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

python实现nao机器人手臂动作控制

本文实例为大家分享了python实现nao机器人手臂动作的具体代码,供大家参考,具体内容如下 这些天依然在看nao公司文档的东西,把读过的代码顺手敲了出来。代码依然很简单,但是为什么我要...

简单的通用表达式求10乘阶示例

复制代码 代码如下:(lambda x: lambda n: x(x)(n))(lambda f: lambda n: 1 if n == 0 else n*f(f)(n-1))(10)...

python单线程文件传输的实例(C/S)

python单线程文件传输的实例(C/S)

客户端代码: #-*-encoding:utf-8-*- import socket import os import sys import math import time...

详解Python中的正则表达式的用法

详解Python中的正则表达式的用法

如果直接在命令行中利用input和raw_input读入一个文件来处理,并且想要采用直接将文件拖入命令行来处理的方式, input方法可以直接处理,而如果要采用raw_input的方法的...

python自动12306抢票软件实现代码

昨天我发的是抓取的12306数据包,然后分析了一下,今天按照昨天的分析 用代码实现了,如果有需要的同学们可以看一下,实现的功能有,登录,验证码识别,自动查票,有余票点击预定, 差了最后一...