在CONF文件夹下
settings.py
1 import os,sys 2 import logging 3 4 BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 5 6 LOG_LEVEL = logging.DEBUG 7 8 LOG_TYPE = { 9 'text1':'text1.log', 10 'text2':'text2.log', 11 }
在CORE文件夹下
logger.py 文件内容
1 import logging 2 from conf import setting 3 4 5 def logger(log_type): 6 7 logger = logging.getLogger(log_type) 8 logger.setLevel(setting.LOG_LEVEL) 9 10 fh = logging.FileHandler('%s/db/%s'%(setting.BASE_PATH,setting.LOG_TYPE[log_type])) 11 fh.setLevel(setting.LOG_LEVEL) 12 13 format_file = logging.Formatter('%(asctime)s - %(name)s - %(message)s') 14 15 fh.setFormatter(format_file) 16 17 logger.addHandler(fh) 18 19 return logger
在根目录下
start.py
1 from core import logger 2 3 if __name__ == '__main__': 4 log1 = logger.logger('text1') 5 log2 = logger.logger('text2') 6 7 log2.info('写入日志2中') 8 log1.info('写入日志1中')