python中logging模块详解_python的logging模块详解

日志级别>>>import logging

>>>logging.NOTSET

0

>>>logging.DEBUG

10

>>>logging.INFO

20

>>>logging.WARN

30

>>>logging.ERROR

40

>>>logging.CRITICAL

50

>>>logging._levelNames

{0:'NOTSET', 10: 'DEBUG', 'WARN': 30, 20: 'INFO', 'ERROR': 40, 'DEBUG': 10, 30:'WARNING', 'INFO': 20, 'WARNING': 30, 40: 'ERROR', 50: 'CRITICAL', 'CRITICAL':50, 'NOTSET': 0}

从以上可以看出logging模块输出日志默认共有6个级别,级别大小依次是CRITICAL>ERROR> WARN> INFO> DEBUG> NOTSET

日志默认输出级别[root@ju tmp]# cat log.py

#!/usr/bin/envpython

importlogging

logging.debug('Thisis debug level')

logging.info('Thisis info level')

logging.warning('Thisis warning level')

logging.error('Thisis error level')

logging.critical('Thisis critical level')

输出结果如下:[root@ju tmp]# python log.py

WARNING:root:Thisis warning level

ERROR:root:Thisis error level

CRITICAL:root:Thisis critical level

从执行结果可以看出,logging模块的日志输出级别默认是warning级别。

自定义日志格式并输出到文件

日志的输出格式和方式可以通过logging.basicConfig函数配置。#!/usr/bin/envpython

#coding=utf-8

importlogging

logging.basicConfig(level=logging.DEBUG,

format='%(asctime)s%(filename)s[line:%(lineno)d] %(levelname)s %(message)s',

datefmt='%a, %d %b %Y %H:%M:%S',

filename='test.log',

filemode='a')

'''

可见在logging.basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有:

filename:          指定日志文件名

filemode:          指定日志文件的打开模式,'w'覆盖或'a'追加

format:            定义输出的格式和内容

datefmt:           指定时间格式,同time.strftime()

level:            设置rootlogger的日志级别

stream:            用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件,默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。

'''

logging.debug('Thisis debug level')

logging.info('Thisis info level')

logging.warning('Thisis warning level')

logging.error('Thisis error level')

logging.critical('Thisis critical level')

[root@ju python_moudle]# python logtest.py

输出结果如下:

[root@ju python_moudle]# cat test.log

Mon, 15Jun 2015 03:48:01 logtest.py[line:10] DEBUG This is debug level

Mon, 15Jun 2015 03:48:01 logtest.py[line:11] INFO This is info level

Mon, 15Jun 2015 03:48:01 logtest.py[line:12] WARNING This is warning level

Mon, 15Jun 2015 03:48:01 logtest.py[line:13] ERROR This is error level

Mon, 15Jun 2015 03:48:01 logtest.py[line:14] CRITICAL This is critical level

自定义日志格式并输出到控制台[root@ju python_moudle]# cat logtest.py

#!/usr/bin/envpython

#coding=utf-8

importlogging

#定义一个StreamHandler,将INFO级别或更高的日志信息打印到标准错误,并将其添加到当前的日志处理对象#

console= logging.StreamHandler()  #StreamHandler输出到控制台,FileHandler输出到文件.

console.setLevel(logging.WARN)

formatter= logging.Formatter('%(name)-8s: %(levelname)-12s %(message)s')

console.setFormatter(formatter)

logging.getLogger('').addHandler(console)

logging.debug('Thisis debug level')

logging.info('Thisis info level')

logging.warning('Thisis warning level')

logging.error('Thisis error level')

logging.critical('Thisis critical level')

输出结果如下:[root@ju python_moudle]# python logtest.py

root    : WARNING      This is warning level

root    : ERROR         Thisis error level

root    : CRITICAL      Thisis critical level

Handlers

handler对象负责发送相关的信息到指定目的地。Python的日志系统有多种Handler可以使用。StreamHandler可以把信息输出到控制台,FileHandler可以把信息输出到文件,还有些Handler可以把信息发送到网络上。如果觉得不够用,还可以编写自己的Handler。可以通过addHandler()方法添加多个多handler。

Handler.setLevel(levelname):指定被处理的信息级别,低于levelname级别的信息将被忽略

Handler.setFormatter():给这个handler指定一个格式

Handler.addFilter(filt)、Handler.removeFilter(filt):新增或删除一个filter对象

Logging模块中有多中可用的Handler:

logging.StreamHandler可以向类似与sys.stdout或者sys.stderr的任何文件对象(file object)输出信息

logging.FileHandler用于向一个文件输出日志信息

logging.handlers.RotatingFileHandler类似于上面的FileHandler,但是它可以管理文件大小。当文件达到一定大小之后,它会自动将当前日志文件改名,然后创建一个新的同名日志文件继续输出

logging.handlers.TimedRotatingFileHandler和RotatingFileHandler类似,不过,它没有通过判断文件大小来决定何时重新创建日志文件,而是间隔一定时间就自动创建新的日志文件

logging.handlers.SocketHandler使用TCP协议,将日志信息发送到网络。

logging.handlers.DatagramHandler使用UDP协议,将日志信息发送到网络。

logging.handlers.SysLogHandler日志输出到syslog

logging.handlers.NTEventLogHandler远程输出日志到Windows NT/2000/XP的事件日志

logging.handlers.SMTPHandler远程输出日志到邮件地址

logging.handlers.MemoryHandler日志输出到内存中的制定buffer

logging.handlers.HTTPHandler通过"GET"或"POST"远程输出到HTTP服务器

各个Handler的具体用法可查看参考书册:

Formatters

Formatter对象设置日志信息最后的规则、结构和内容,默认的时间格式为%Y-%m-%d%H:%M:%S,下面是Formatter常用的一些信息。

%(name)s                  Logger的名字

%(levelno)s:打印日志级别的数值

%(levelname)s:打印日志级别名称

%(pathname)s:打印当前执行程序的路径,其实就是sys.argv[0]

%(filename)s:打印当前执行程序名

%(module)s调用日志输出函数的模块名

%(funcName)s:打印日志的当前函数

%(lineno)d:打印日志的当前行号

%(created)f当前时间,用UNIX标准的表示时间的浮点数表示

%(relativeCreated)d输出日志信息时的,自Logger创建以来的毫秒数

%(asctime)s:打印日志的时间

%(thread)d:打印线程ID

%(threadName)s:打印线程名称

%(process)d:打印进程ID

%(message)s:打印日志信息

getLogger模块详解

logging.getLogger()时参数的格式类似于“arg1.arg2.arg3”,上代码:[root@ju python_moudle]# cat logtest.py

#!/usr/bin/envpython

#coding=utf-8

importlogging

#创建一个logger

logroot= logging.getLogger()

log1 =logging.getLogger('L1')

log1.setLevel(logging.DEBUG)

log2 =logging.getLogger('L1.L2')

log2.setLevel(logging.INFO)

log3 = logging.getLogger('L1.L2.L3')

log3.setLevel(logging.WARNING)

#创建一个handler,用于输出到控制台

ch =logging.StreamHandler()

#定义handler的输出格式formatter

formatter= logging.Formatter('%(name)s - %(levelname)s - %(message)s')

ch.setFormatter(formatter)

#给logger添加handler

logroot.addHandler(ch)

log1.addHandler(ch)

log2.addHandler(ch)

log3.addHandler(ch)

#打印日志

logroot.debug('logrootdebug message')

logroot.info('logrootinfo message')

logroot.warning('logrootwarning message')

logroot.error('logrooterror message')

logroot.critical('logrootcritical message')

log1.debug('log1debug message')

log1.info('log1info message')

log1.warning('log1warning message')

log1.error('log1error message')

log1.critical('log1critical message')

log2.debug('log2debug message')

log2.info('log2info message')

log2.warning('log2warning message')

log2.error('log2error message')

log2.critical('log2critical message')

log3.debug('log3debug message')

log3.info('log3info message')

log3.warning('log3warning message')

log3.error('log3error message')

log3.critical('log3critical message')

输出结果如下:[root@jupython_moudle]# python logtest.py

root -WARNING - logroot warning message

root -ERROR - logroot error message

root -CRITICAL - logroot critical message

L1 -DEBUG - log1 debug message

L1 -DEBUG - log1 debug message

L1 -INFO - log1 info message

L1 -INFO - log1 info message

L1 -WARNING - log1 warning message

L1 -WARNING - log1 warning message

L1 -ERROR - log1 error message

L1 -ERROR - log1 error message

L1 -CRITICAL - log1 critical message

L1 -CRITICAL - log1 critical message

L1.L2 -INFO - log2 info message

L1.L2 -INFO - log2 info message

L1.L2 -INFO - log2 info message

L1.L2 -WARNING - log2 warning message

L1.L2 -WARNING - log2 warning message

L1.L2 -WARNING - log2 warning message

L1.L2 -ERROR - log2 error message

L1.L2 -ERROR - log2 error message

L1.L2 -ERROR - log2 error message

L1.L2 -CRITICAL - log2 critical message

L1.L2 -CRITICAL - log2 critical message

L1.L2 -CRITICAL - log2 critical message

L1.L2.L3- WARNING - log3 warning message

L1.L2.L3- WARNING - log3 warning message

L1.L2.L3- WARNING - log3 warning message

L1.L2.L3- WARNING - log3 warning message

L1.L2.L3- ERROR - log3 error message

L1.L2.L3- ERROR - log3 error message

L1.L2.L3- ERROR - log3 error message

L1.L2.L3- ERROR - log3 error message

L1.L2.L3- CRITICAL - log3 critical message

L1.L2.L3- CRITICAL - log3 critical message

L1.L2.L3- CRITICAL - log3 critical message

L1.L2.L3- CRITICAL - log3 critical message

由以上输出结果可以看出rootlog的每个输出分别打印1次,log1对应的输出分别打印了2次,log2对应的输出打印了3次,log3打印4次。logger = logging.getLogger()显示的创建了root Logger,这是因为logger实例之间的“父子关系”,rootlogger处于最顶层,log1是第二层,log2是第三层,以此类推。所以在把消息分发给它们的handler进行处理时也会传递给所有的祖先Logger处理。

现在把以下两行注释,再输出logroot.addHandler(ch)

log1.addHandler(ch)

输出结果如下:[root@ju python_moudle]# python logtest.py

Nohandlers could be found for logger "root"

L1.L2 -INFO - log2 info message

L1.L2 -WARNING - log2 warning message

L1.L2 -ERROR - log2 error message

L1.L2 -CRITICAL - log2 critical message

L1.L2.L3- WARNING - log3 warning message

L1.L2.L3- WARNING - log3 warning message

L1.L2.L3- ERROR - log3 error message

L1.L2.L3- ERROR - log3 error message

L1.L2.L3- CRITICAL - log3 critical message

L1.L2.L3- CRITICAL - log3 critical message

这时候可以看到log2就变成最顶层的节点,log3在handler时还是要传递给log2处理一遍,所以输出两遍。

使用filter设置过滤器

限制只有满足过滤规则的日志才会输出。比如我们定义了filter = logging.Filter('L1.L2.L3'),并将这个Filter添加到了一个Handler上,则使用该Handler的Logger中只有名字带a.b.c前缀的Logger才能输出其日志。看代码:[root@jupython_moudle]# cat logtest.py

#!/usr/bin/envpython

#coding=utf-8

importlogging

#创建一个logger

logroot= logging.getLogger()

log1 =logging.getLogger('L1')

log1.setLevel(logging.DEBUG)

log2 =logging.getLogger('L1.L2')

log2.setLevel(logging.INFO)

log3 =logging.getLogger('L1.L2.L3')

log3.setLevel(logging.WARNING)

#再创建一个handler,用于输出到控制台

ch =logging.StreamHandler()

#定义handler的输出格式formatter

formatter= logging.Formatter('%(name)s - %(levelname)s - %(message)s')

ch.setFormatter(formatter)

#定义filter

f =logging.Filter('L1.L2.L3')

ch.addFilter(f)

#给logger添加handler

logroot.addHandler(ch)

log1.addHandler(ch)

log2.addHandler(ch)

log3.addHandler(ch)

#打印日志

logroot.debug('logrootdebug message')

logroot.info('logrootinfo message')

logroot.warning('logrootwarning message')

logroot.error('logrooterror message')

logroot.critical('logrootcritical message')

log1.debug('log1debug message')

log1.info('log1info message')

log1.warning('log1warning message')

log1.error('log1error message')

log1.critical('log1critical message')

log2.debug('log2debug message')

log2.info('log2info message')

log2.warning('log2warning message')

log2.error('log2error message')

log2.critical('log2critical message')

log3.debug('log3debug message')

log3.info('log3info message')

log3.warning('log3warning message')

log3.error('log3error message')

log3.critical('log3critical message')

输出结果如下:[root@ju python_moudle]# python logtest.py

L1.L2.L3- WARNING - log3 warning message

L1.L2.L3- WARNING - log3 warning message

L1.L2.L3- WARNING - log3 warning message

L1.L2.L3- WARNING - log3 warning message

L1.L2.L3- ERROR - log3 error message

L1.L2.L3- ERROR - log3 error message

L1.L2.L3- ERROR - log3 error message

L1.L2.L3- ERROR - log3 error message

L1.L2.L3- CRITICAL - log3 critical message

L1.L2.L3- CRITICAL - log3 critical message

L1.L2.L3- CRITICAL - log3 critical message

L1.L2.L3- CRITICAL - log3 critical message

由输出结果可以看到,正如前面所说,只输出以L1.L2.L3开头的logger了。但是为Handler加Filter后只要使用了该Handler的Logger都会受影响。而为Logger单独添加Filter只会影响到自身。看代码:#定义filter

f =logging.Filter('L1.L2')

#ch.addFilter(f)                   #把此行注释

#给logger添加filter和handler

logroot.addHandler(ch)

log1.addFilter(f)                  #给log1加上filter

log1.addHandler(ch)

log2.addFilter(f)                  #给log2加上filter

log2.addHandler(ch)

log3.addHandler(ch)

输出结果如下:[root@ju python_moudle]# python logtest.py

root -WARNING - logroot warning message

root -ERROR - logroot error message

root -CRITICAL - logroot critical message

L1.L2 -INFO - log2 info message

L1.L2 -INFO - log2 info message

L1.L2 -INFO - log2 info message

L1.L2 -WARNING - log2 warning message

L1.L2 -WARNING - log2 warning message

L1.L2 -WARNING - log2 warning message

L1.L2 -ERROR - log2 error message

L1.L2 -ERROR - log2 error message

L1.L2 -ERROR - log2 error message

L1.L2 -CRITICAL - log2 critical message

L1.L2 -CRITICAL - log2 critical message

L1.L2 -CRITICAL - log2 critical message

L1.L2.L3- WARNING - log3 warning message

L1.L2.L3- WARNING - log3 warning message

L1.L2.L3- WARNING - log3 warning message

L1.L2.L3- WARNING - log3 warning message

L1.L2.L3- ERROR - log3 error message

L1.L2.L3- ERROR - log3 error message

L1.L2.L3- ERROR - log3 error message

L1.L2.L3- ERROR - log3 error message

L1.L2.L3- CRITICAL - log3 critical message

L1.L2.L3- CRITICAL - log3 critical message

L1.L2.L3- CRITICAL - log3 critical message

L1.L2.L3- CRITICAL - log3 critical message

由以上输出可以看到rootlog并未受影响,log1由于未匹配到所以没有输出,log2匹配到说以输出,log3未定义filter也可能输出。

通过logging.config模块配置日志[root@jupython_moudle]# cat log.conf

[loggers]

keys=root,TestLogger

[handlers]

keys=consoleHandler

[formatters]

keys=TestFormatter

[logger_root]

level=DEBUG

handlers=consoleHandler

[logger_TestLogger]

level=DEBUG

handlers=consoleHandler

qualname=TestLogger

propagate=0

[handler_consoleHandler]

class=StreamHandler

level=DEBUG

formatter=TestFormatter

args=(sys.stdout,)

[formatter_TestFormatter]

format=%(asctime)-12s%(name)-12s %(levelname)-12s %(message)s

datefmt='%a,%d %b %Y %H:%M:%S'

[root@jupython_moudle]# cat logtest.py

#!/usr/bin/envpython

#coding=utf-8

importlogging

importlogging.config

logging.config.fileConfig('log.conf')  #使用log.conf配置文件

#创建日志

LOG =logging.getLogger("TestLogger")

LOG.debug('Thisis debug level')

LOG.info('Thisis info level')

LOG.warning('Thisis warning level')

LOG.error('Thisis error level')

LOG.critical('Thisis critical level')

结果输出如下:[root@ju python_moudle]# python logtest.py

'Mon, 15Jun 2015 08:04:49' TestLogger  DEBUG        This is debug level

'Mon, 15Jun 2015 08:04:49' TestLogger   INFO         This is info level

'Mon, 15Jun 2015 08:04:49' TestLogger  WARNING      This is warning level

'Mon, 15Jun 2015 08:04:49' TestLogger  ERROR        This is error level

'Mon, 15Jun 2015 08:04:49' TestLogger  CRITICAL     This is criticallevel

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值