python 中logging模块

logging的作用:python中,logging模块主要是处理日志的。所谓日志,可理解为在软件运行过程中,所记录的的一些运行情况信息,软件开发人员可以根据自己的需求添加日志,日志可以帮助软件开发人员了解软件的运行信息。

【1】通过logging.basicConfig函数对日志的输出格式及方式做相关配置

import logging
import sys


# 输出不同级别的日志级别
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a,%Y %m %d %H:%M:%S',
                    filename='test.log',
                    filemode='w')#每次运行后都会覆盖原先的记录,a的是添加
# 输出结果:Fri,2018 06 08 08:52:07 logging.py[line:34] DEBUG this is debug info
# 
# 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被忽略



logging.debug('this is debug info')
logging.info('this is information')
logging.warning('this is warning message')
logging.error('this is error message')
logging.fatal('this is fatal message, it is same as logger.critical')
logging.critical('this is critical message')

【2】将日志输出到文件和控制台

import logging
import sys


# 获取logger实例,如果参数为空则返回root logger
logger=logging.getLogger()
#创建一个FileHandler,用于写入日志文件
fh=logging.FileHandler('mylog.log')
#再创建一个handler,用于输出到控制台
ch=logging.StreamHandler()
## 指定logger输出格式
formatter=logging.Formatter('%(asctime)s-%(name)s-%(levelname)s-%(message)s')
#可以通过setFormatter指定输出格式
fh.setFormatter(formatter)# 文件日志
ch.setFormatter(formatter)# 控制台日志
# 为logger添加的日志处理器
logger.addHandler(fh)
logger.addHandler(ch)

# 指定日志的最低输出级别,默认为WARN级别
logger.setLevel(logging.DEBUG)
logger.debug('this is debug info')
logger.info('this is information')
logger.warning('this is warning message')
logger.error('this is error message')
logger.critical('this is critical message')

【3】logging日志之回滚

import logging
from logging.handlers import RotatingFileHandler

#################################################################################################
#定义一个RotatingFileHandler,最多备份5个日志文件,每个日志文件最大10M
Rthandler = 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.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服务器

猜你喜欢

转载自www.cnblogs.com/lanyinhao/p/9154025.html