python logging模块总结
来源:互联网 发布:数据库关系模式的定义 编辑:程序博客网 时间:2024/06/05 22:54
一、将日志打印到屏幕
import logginglogging.debug('This is debug message')logging.info('This is info message')logging.warning('This is warning message')屏幕输出为:
WARNING:root:This is warning message默认情况下,logging将日志打印到屏幕,日志级别为WARNING;
日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET,当然也可以自己定义日志级别。
二、通过logging.basicConfig函数对日志的输出格式及方式做相关配置
import logginglogging.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='w')logging.debug('This is debug message')logging.info('This is info message')logging.warning('This is warning message')
./Test.log文件中内容为:
Wed, 20 Sep 2017 09:59:34 Test.py[line:14] DEBUG This is debug messageWed, 20 Sep 2017 09:59:34 Test.py[line:15] INFO This is info messageWed, 20 Sep 2017 09:59:34 Test.py[line:16] WARNING This is warning messagelogging.basicConfig函数各参数:
filename: 指定日志文件名
filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'
format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:
datefmt: 指定时间格式,同time.strftime()
level: 设置日志级别,默认为logging.WARNING
stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略
#-*- coding:utf-8 -*-import logginglogging.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='w')###################################################定义一个StreamHandler,将INFO级别或更高的日志信息打印到标准错误,并将其添加到当前的日志处理对象#console = logging.StreamHandler()console.setLevel(logging.INFO)formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')console.setFormatter(formatter)logging.getLogger('').addHandler(console)##############################################logging.debug('This is debug message')logging.info('This is info message')logging.warning('This is warning message')屏幕输出为:
root : INFO This is info messageroot : WARNING This is warning message
./Test.log文件输出为:
Wed, 20 Sep 2017 10:17:29 Test.py[line:21] DEBUG This is debug messageWed, 20 Sep 2017 10:17:29 Test.py[line:22] INFO This is info messageWed, 20 Sep 2017 10:17:29 Test.py[line:23] WARNING This is warning message
四、日志回滚import loggingfrom logging.handlers import RotatingFileHandler##################################################################################################定义一个RotatingFileHandler,最多备份5个日志文件,每个日志文件最大10MRthandler = RotatingFileHandler('myapp.log', maxBytes=10*1024*1024,backupCount=5)Rthandler.setLevel(logging.INFO)formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')Rthandler.setFormatter(formatter)logging.getLogger('').addHandler(Rthandler)################################################################################################
从上例和本例可以看出,logging有一个日志处理的主对象,其它处理方式都是通过addHandler添加进去的。logging的几种handle方式如下:
logging.StreamHandler: 日志输出到流,可以是sys.stderr、sys.stdout或者文件
logging.FileHandler: 日志输出到文件
日志回滚方式,实际使用时用RotatingFileHandler和TimedRotatingFileHandler
logging.handlers.BaseRotatingHandler
logging.handlers.RotatingFileHandler
logging.handlers.TimedRotatingFileHandler
logging.handlers.SocketHandler: 远程输出日志到TCP/IP sockets
logging.handlers.DatagramHandler:
logging.handlers.SMTPHandler:
logging.handlers.SysLogHandler: 日志输出到syslog
logging.handlers.NTEventLogHandler: 远程输出日志到Windows NT/2000/XP的事件日志
logging.handlers.MemoryHandler: 日志输出到内存中的制定buffer
logging.handlers.HTTPHandler: 通过"GET"或"POST"远程输出到HTTP服务器
上述其它处理方式的使用请参见python2.5手册!
五、通过logging.config模块配置日志
#logger.conf###############################################[loggers]keys=root,example01,example02[logger_root]level=DEBUGhandlers=hand01,hand02[logger_example01]handlers=hand01,hand02qualname=example01propagate=0[logger_example02]handlers=hand01,hand03qualname=example02propagate=0###############################################[handlers]keys=hand01,hand02,hand03[handler_hand01]class=StreamHandlerlevel=INFOformatter=form02args=(sys.stderr,)[handler_hand02]class=FileHandlerlevel=DEBUGformatter=form01args=('myapp.log', 'a')[handler_hand03]class=handlers.RotatingFileHandlerlevel=INFOformatter=form02args=('myapp.log', 'a', 10*1024*1024, 5)###############################################[formatters]keys=form01,form02[formatter_form01]format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)sdatefmt=%a, %d %b %Y %H:%M:%S[formatter_form02]format=%(name)-12s: %(levelname)-8s %(message)sdatefmt=上例3:
import loggingimport logging.configlogging.config.fileConfig("logger.conf")logger = logging.getLogger("example01")logger.debug('This is debug message')logger.info('This is info message')logger.warning('This is warning message')上例4:
import loggingimport logging.configlogging.config.fileConfig("logger.conf")logger = logging.getLogger("example02")logger.debug('This is debug message')logger.info('This is info message')logger.warning('This is warning message')
- python logging模块总结
- python的日志logging模块使用总结
- python的日志logging模块使用总结
- python的日志logging模块使用总结
- python logging 模块常见使用需求总结
- python模块 ---logging模块
- python logging模块学习
- Python标准模块logging
- Python标准模块logging
- python logging模块
- python 之logging模块
- python的logging模块
- Python标准模块logging
- [python相关]logging模块
- Python标准模块logging
- python logging模块学习
- Python标准模块logging
- Python日志模块-logging
- Ubuntu 16.04 LTS 成功编译 Android 6.0 源码教程
- Leetcode282. Expression Add Operators
- spring Quartz windows上执行 linux不执行原因
- Android媒体应用(一)
- java之ProcessBuilder执行系统命令
- python logging模块总结
- 向虚拟客服发脾气?你无意中推动了 AI 的进步
- 图像分析:投影曲线的波峰查找
- Java 常见异常种类
- matlab版本下载地址
- 微信开发写入log日志-微信开发教程7
- php正则表达式手册
- java的varargs机制
- 【转】【DBSDFZOJ 1163】分治 第K小元素(分治)