python日志模块---logging

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

1.将日志打印到屏幕1importlogging23logging.debug('This is debug message---by liu-ke')4logging.info('This is info message---by liu-ke')5logging.warning('This is warning message---by liu-ke')

默认情况下,logging将日志打印到屏幕,日志级别为WARNING;

日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET,日志级别可以自己定义。

2.通过logging.basicConfig函数 配置 日志的输出格式及方式1importlogging23logging.basicConfig(level=logging.DEBUG,4format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',5datefmt='%a, %d %b %Y %H:%M:%S',6filename='mypy.log',7filemode='w')89logging.debug('This is debug message by liu-ke')10logging.info('This is info message by liu-ke')11logging.warning('This is warning message by liu-ke')

1logging.basicConfig函数各参数:2filename: 指定日志文件名3filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'4format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:5%(levelno)s: 打印日志级别的数值6%(levelname)s: 打印日志级别名称7%(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]8%(filename)s: 打印当前执行程序名9%(funcName)s: 打印日志的当前函数10%(lineno)d: 打印日志的当前行号11%(asctime)s: 打印日志的时间12%(thread)d: 打印线程ID13%(threadName)s: 打印线程名称14%(PRocess)d: 打印进程ID15%(message)s: 打印日志信息16datefmt: 指定时间格式,同time.strftime()17level: 设置日志级别,默认为logging.WARNING18stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略

3.将日志同时输出到文件和屏幕1importlogging23logging.basicConfig(level=logging.DEBUG,4format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',5datefmt='%a, %d %b %Y %H:%M:%S',6filename='mypy.log',7filemode='w')89#################################################################################################10#定义一个StreamHandler,将INFO级别或更高的日志信息打印到标准错误,并将其添加到当前的日志处理对象#11console =logging.StreamHandler()12console.setLevel(logging.INFO)13formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')14console.setFormatter(formatter)15logging.getLogger('').addHandler(console)16#################################################################################################1718logging.debug('This is debug message')19logging.info('This is info message')20logging.warning('This is warning message')

4.logging之日志回滚1importlogging2fromlogging.handlersimportRotatingFileHandler34#################################################################################################5#定义一个RotatingFileHandler,最多备份5个日志文件,每个日志文件最大10M(自定义)6Rthandler = RotatingFileHandler('myapp.log', maxBytes=10*1024*1024,backupCount=5)7Rthandler.setLevel(logging.INFO)8formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')9Rthandler.setFormatter(formatter)10logging.getLogger('').addHandler(Rthandler)11###########################################################################################

从上例和本例可以看出,logging有一个日志处理的主对象,其它处理方式都是通过addHandler添加进去的。

logging的几种handle方式如下:

1logging.StreamHandler: 日志输出到流,可以是sys.stderr、sys.stdout或者文件2logging.FileHandler: 日志输出到文件3日志回滚方式,实际使用时用RotatingFileHandler和TimedRotatingFileHandler4logging.handlers.BaseRotatingHandler5logging.handlers.RotatingFileHandler6logging.handlers.TimedRotatingFileHandler7logging.handlers.SocketHandler: 远程输出日志到TCP/ipsockets8logging.handlers.DatagramHandler: 远程输出日志到UDP sockets9logging.handlers.SMTPHandler: 远程输出日志到邮件地址10logging.handlers.SysLogHandler: 日志输出到syslog11logging.handlers.NTEventLogHandler: 远程输出日志到Windows NT/2000/XP的事件日志12logging.handlers.MemoryHandler: 日志输出到内存中的制定buffer13logging.handlers.HTTPHandler: 通过"GET"或"POST"远程输出到HTTP服务器

由于StreamHandler和FileHandler是常用的日志处理方式,所以直接包含在logging模块中,而其他方式则包含在logging.handlers模块中,

上述其它处理方式的使用请自行参见python2.7手册!

5.通过logging.config模块配置日志1#logger.conf2###############################################3[loggers]4keys=root,example01,example025[logger_root]6level=DEBUG7handlers=hand01,hand028[logger_example01]9handlers=hand01,hand0210qualname=example0111propagate=012[logger_example02]13handlers=hand01,hand0314qualname=example0215propagate=016###############################################17[handlers]18keys=hand01,hand02,hand0319[handler_hand01]20class=StreamHandler21level=INFO22formatter=form0223args=(sys.stderr,)24[handler_hand02]25class=FileHandler26level=DEBUG27formatter=form0128args=('myapp.log','a')29[handler_hand03]30class=handlers.RotatingFileHandler31level=INFO32formatter=form0233args=('myapp.log','a', 10*1024*1024, 5)34###############################################35[formatters]36keys=form01,form0237[formatter_form01]38format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s39datefmt=%a, %d %b %Y %H:%M:%S40[formatter_form02]41format=%(name)-12s: %(levelname)-8s %(message)s42datefmt=

上例3:

1importlogging2importlogging.config34logging.config.fileConfig("logger.conf")5logger = logging.getLogger("example01")67logger.debug('This is debug message')8logger.info('This is info message')9logger.warning('This is warning message')

上例4:

1importlogging2importlogging.config34logging.config.fileConfig("logger.conf")5logger = logging.getLogger("example02")67logger.debug('This is debug message')8logger.info('This is info message')9logger.warning('This is warning message')

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航