1. 程式人生 > >python基礎之ATM-5

python基礎之ATM-5

asc ack and import 文件備份 pen eve from message

import logging
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from conf import settings
from logging import handlers
‘‘‘
本模塊是logging模塊,實現的日誌的記錄
‘‘‘

def write_logger(log_params,info):
logger = logging.getLogger(‘ATM_LOG‘)
logger.setLevel(logging.DEBUG)
log_path = log_handler(log_params)
log_file = ‘%s.log‘ % (log_path)
# fh = logging.FileHandler(log_file) #普通
# fh = handlers.RotatingFileHandler(filename=log_file,maxBytes=10,backupCount=3) #按大小備份
fh = handlers.TimedRotatingFileHandler(filename=log_file, when="midnight", interval=5, backupCount=3) #每日淩晨生成一個日誌文件,原文件備份
fh.setLevel(logging.DEBUG)
fh_formatter = logging.Formatter(‘%(asctime)s %(levelname)s:%(message)s‘)
fh.setFormatter(fh_formatter)
logger.addHandler(fh)
logger.debug(info)
logger.removeHandler(fh)

def file_log_handler(log_params):
log_path = ‘%s\%s‘%(log_params["path"],log_params["name"])
return log_path

def log_handler(log_params):
if log_params["file"] == "data":
return file_log_handler(log_params)
elif log_params["file"] == "trans":
return file_log_handler(log_params)

python基礎之ATM-5