python模块详解 logging

打印日志的五个级别:

import logging

logging.debug('test debug') logging.info('test info') logging.warning('test warning')# WARNING:root:test warning logging.error('test error') # ERROR:root:test error logging.critical('test critical')# CRITICAL:root:test critical

 把日志信息写入到文件:

import logging
logging.basicConfig(filename='app.log',level=logging.DEBUG)
logging.debug('test debug')
logging.info('test info')
logging.warning('test warning')
logging.error('test error')
logging.critical('test critical')

 app.log

DEBUG:root:test debug
INFO:root:test info
WARNING:root:test warning
ERROR:root:test error
CRITICAL:root:test critical

 添加日志添加时间:

import logging
logging.basicConfig(filename='app.log',level=logging.DEBUG,format='%(asctime)s %(message)s',datefmt='%Y/%m/%d %I:%M:%S %p') 
#设置写入的文件、默认级别 (小于这个级别的不写入)、输出格式和时间 logging.debug('test debug') # 2017/08/05 05:02:31 PM test debug logging.info('test info') # 2017/08/05 05:02:31 PM test info logging.warning('test warning') # 2017/08/05 05:02:31 PM test warning logging.error('test error') # 2017/08/05 05:02:31 PM test error logging.critical('test critical') # 2017/08/05 05:02:31 PM test critical

 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被忽略

将日志同时输出到文件和屏幕

import logging

logger = logging.getLogger('TEST-LOG')
logger.setLevel(logging.DEBUG)

ch = logging.StreamHandler() #设置屏幕的handler
ch.setLevel(logging.WARNING)

fh = logging.FileHandler('access.log',encoding="utf-8")  #设置输出文件的handler
fh.setLevel(logging.ERROR)
#设置两种输出格式
fh_formatter =logging.Formatter('%(asctime)s %(process)d %(filename)s %(levelname)s %(message)s')
ch_formatter =logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Handler和Formatter关联
fh.setFormatter(fh_formatter)
ch.setFormatter(ch_formatter)
#添加到logger
logger.addHandler(fh)
logger.addHandler(ch)

logger.warning('warning')#2017-08-07 15:15:25,693 - TEST-LOG - WARNING - warning
logger.error('error')#2017-08-07 15:15:25,693 - TEST-LOG - ERROR - error

 access.log

2017-08-07 15:15:25,693 5380 01.py ERROR error

自动截断

import logging
from logging import handlers
logger = logging.getLogger("TEST")

log_file = 'timelog.log'
fh = handlers.RotatingFileHandler(filename=log_file,maxBytes=10,backupCount=3,encoding="utf-8")
#fh = handlers.TimedRotatingFileHandler(filename=log_file,when="S",interval=5,backupCount=3)

formatter = logging.Formatter('%(asctime)s %(module)s:%(lineno)d %(message)s')

fh.setFormatter(formatter)

logger.addHandler(fh)

logger.warning('test11')
logger.warning('test12')
logger.warning('test13')
logger.warning('test14')

 timelog.log

2017-08-07 15:41:48,742 02:19 test14

timelog.log1

2017-08-07 15:41:48,735 02:18 test13

timelog.log2

2017-08-07 15:41:48,715 02:17 test12

timelog.log3

2017-08-07 15:41:48,712 02:16 test11

其它详细操作:python 的日志logging模块学习

原文地址:https://www.cnblogs.com/qing-chen/p/7290805.html