Python 日志记录
# coding=utf-8
import os
import sys,pdb
import logbook #pip install Logbook
from logbook import Logger,StreamHandler,FileHandler,TimedRotatingFileHandler
from logbook.more import ColorizedStderrHandler
import settings
class my_logbook(Logger):
def __init__(self,name,logPrefix=settings.APPNAME,bOutStd=True):
# 日志存放路径
Logger.__init__(self,name)
LOG_DIR = os.path.join(settings.LOGPATH)
if not os.path.exists(LOG_DIR):
os.makedirs(LOG_DIR)
pdb.set_trace()
# 日志打印到屏幕
log_std = ColorizedStderrHandler(bubble=bOutStd)
log_std.formatter = log_type
# 日志打印到文件
log_file = TimedRotatingFileHandler(
os.path.join(LOG_DIR, '%s.log' % logPrefix),date_format='%Y-%m-%d', bubble=bOutStd, encoding='utf-8')
log_file.formatter = log_type
logbook.set_datetime_format("local")
self.handlers = []
self.handlers.append(log_file)
self.handlers.append(log_std)
def log_type(record,handler):
log = "[{date}] {msg}".format(
date = record.time, # 日志时间
msg = record.message # 日志内容
)
'''
log = "[{date}] [{level}] [{filename}] [{func_name}] [{lineno}] {msg}".format(
date = record.time, # 日志时间
level = record.level_name, # 日志等级
filename = os.path.split(record.filename)[-1], # 文件名
func_name = record.func_name, # 函数名
lineno = record.lineno, # 行号
msg = record.message # 日志内容
)
'''
return log
logging = my_logbook("script_log",settings.APPNAME,bOutStd=False)
# logging.info('开始下载所需数据......')