跑程序犹如走人生,有些美好的、不美好的事需要记录下来,慢慢斟酌~
logging模块由此而生,记录那些你想记录的事~
一、将日志打印到屏幕
import logging
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')
# 屏幕上打印
# 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 logging
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='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 information
logging.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 logging
from logging import FileHandler
LOG_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服务器