1 import logging.config 2 import logging 3 import configparser 4 import codecs 5 class Log(object): 6 def __init__(self): 7 cp = configparser.SafeConfigParser() 8 cp.read(‘logging_config.ini‘) 9 10 self.level = cp.get(‘logging‘,‘level‘) 11 self.format = cp.get(‘logging‘,‘format‘) 12 self.fmt = self.format.replace(‘|‘,‘%‘) 13 self.log_file = cp.get(‘logging‘,‘log_file‘) 14 self.console_log_on = cp.getint(‘logging‘,‘console_log_on‘) 15 self.logfile_log_on = cp.getint(‘logging‘,‘logfile_log_on‘) 16 self.logger = logging.getLogger(‘test‘) 17 18 # 设置文件输出log 19 fh = logging.FileHandler(self.log_file) 20 # 设置输入的格式 21 formatter = logging.Formatter(self.fmt) 22 fh.setFormatter(formatter) 23 self.logger.addHandler(fh) 24 25 26 self.logger.setLevel(self.level) 27 # 设置文件输出log 28 sh = logging.StreamHandler() 29 # 设置输入的格式 30 formatter = logging.Formatter(self.fmt) 31 sh.setFormatter(formatter) 32 self.logger.addHandler(sh) 33 self.logger.setLevel(self.level) 34 35 def get_log(self): 36 return self.logger 37 38 log = Log().get_log()
时间: 2024-10-23 05:36:11