您的位置:首页 > 编程语言 > Python开发

python第三方库系列之七--logging库

2014-11-24 23:26 204 查看
跑程序犹如走人生,有些美好的、不美好的事需要记录下来,慢慢斟酌~

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可以输出很多有用信息,如上例所示:

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

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

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

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

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

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

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

%(thread)d: 打印线程ID

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

%(process)d: 打印进程ID

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

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: 远程输出日志到UDP sockets

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

logging.handlers.SysLogHandler: 日志输出到syslog

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

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

logging.handlers.HTTPHandler: 通过"GET"或"POST"远程输出到HTTP服务器
三、logging是线程安全的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: