python第三方库系列之七--logging库
来源:互联网 发布:simulink转换成c语言 编辑:程序博客网 时间:2024/05/18 07:43
跑程序犹如走人生,有些美好的、不美好的事需要记录下来,慢慢斟酌~
logging模块由此而生,记录那些你想记录的事~
一、将日志打印到屏幕
import logginglogging.debug('this is a bug information')logging.info('this is a info information')logging.warning('this is a warning information')logging.error('this is a error information')logging.critical('this is a critical information')# 屏幕上打印# WARNING:root:this is a warning information# ERROR:root:this is a error information# CRITICAL:root:this is a critical information一般的,日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET,当然也可以自己定义日志级别。而且,默认情况下,logging将日志打印到屏幕,日志级别为WARNING,所以,我们写的debug和info被忽略,不会打印。
二、将日志打印到文件
1. 利用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='myapp.log', filemode='w')logging.debug('this is a bug information')logging.info('this is a info information')logging.warning('this is a warning information')logging.error('this is a error information')logging.critical('this is a critical information')# myapp.log文件里打印#Mon, 24 Nov 2014 23:19:32 loggingTest.py[line:11] DEBUG this is a bug information#Mon, 24 Nov 2014 23:19:32 loggingTest.py[line:12] INFO this is a info information#Mon, 24 Nov 2014 23:19:32 loggingTest.py[line:13] WARNING this is a warning information#Mon, 24 Nov 2014 23:19:32 loggingTest.py[line:14] ERROR this is a error information#Mon, 24 Nov 2014 23:19:32 loggingTest.py[line:15] CRITICAL this is a critical informationlogging.basicConfig函数各参数:
filename: 指定日志文件名,如果没有指定就打印到屏幕
filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'
format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:
datefmt: 指定时间格式,同time.strftime()
level: 设置日志级别,默认为logging.WARNING
stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略
2. 利用FileHandler来配置
import loggingfrom logging import FileHandlerLOG_PATH = '/home/tops/adms-agent/logs'logging.basicConfig(level=logging.DEBUG)file_time_handler = FileHandler(LOG_PATH+'/adms-agent.log')file_time_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - {%(filename)s:%(funcName)s:%(lineno)d}: %(message)s"))file_time_handler.suffix = "%Y-%m-%d.log"log = logging.getLogger("adms-agent")log.addHandler(file_time_handler)logging.debug('this is a bug information')logging.info('this is a info information')logging.warning('this is a warning information')logging.error('this is a error information')logging.critical('this is a critical information')
以上只是举个例子用FileHandler来把日志写到文件,其实有很多种日志重定向方法,例如:
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服务器
- python第三方库系列之七--logging库
- python第三方库系列之二十六--virtualenv库
- python第三方库系列之二十三--路径库
- python第三方库系列之二--time库
- python第三方库系列之六--traceback库
- python第三方库系列之八--uuid库
- python第三方库系列之九--os库
- python第三方库系列之十--commands库
- python第三方库系列之十二--多线程threading库
- python第三方库系列之十五--编码库
- python第三方库系列之十七--multiprocessing库
- python第三方库系列之二十五--pip库
- python第三方库系列之二十一--python django的权限管理库
- python第三方库系列之十八--python/django test库
- python第三方库系列之十九--python测试使用的mock库
- python第三方库之numpy
- PyThon第三方库
- 第三方python库
- C++ union用法
- bias and variance在机器学习中的应用
- Ubuntu学习笔记(一)
- Makefile经典教程(掌握这些足够)
- 算法 Merge Sorted Array
- python第三方库系列之七--logging库
- SVM基础篇
- MVC基础入门(二)
- Linux的shell中echo改变输出显示样式
- IOS socket 通信机制的实现
- 算法 Search a 2D Matrix
- jquery clone()函数讲解
- java工程积累——如何处理Tomcat内存溢出
- MD5.SHA.BASE64算法加解密