import logging from logging import handlers from app.app_config import PER_LOG_FILESIZE,LOG_FILE_NUM,LOG_PATH """ desc: 系统日志类 """ class Logger(object): level_relations = { 'debug':logging.DEBUG, 'info':logging.INFO, 'warning':logging.WARNING, 'error':logging.ERROR, 'crit':logging.CRITICAL } def __init__(self,filename = LOG_PATH, level = 'info', when = 'D',backCount = LOG_FILE_NUM, \ fmt='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s', filesize = PER_LOG_FILESIZE): self.logger = logging.getLogger(filename) format_str = logging.Formatter(fmt) self.logger.setLevel(self.level_relations.get(level)) sh = logging.StreamHandler() sh.setFormatter(format_str) th = handlers.RotatingFileHandler( filename = filename, maxBytes=filesize, backupCount=backCount, encoding = 'utf-8') th.setFormatter(format_str) self.logger.addHandler(sh) self.logger.addHandler(th)