作者:我只是狼却有幅羊的心肠_152 | 来源:互联网 | 2023-09-18 20:42
#https:www.cnblogs.comnancyzhup8551506.htmlimportosimporttimeimportloggingPATHlambdap:os
import os
import time
import loggingPATH = lambda p: os.path.abspath(os.path.join(os.path.dirname(__file__), p))
now = time.strftime('%Y%m%d%H%M%S')
log_path = PATH('../log')
if not os.path.exists(log_path): os.mkdir(log_path) class Log(object):"""定义日志类"""def __init__(self, _name):"""初始化logger:param _name: 写每条log的名字"""self.logger = logging.getLogger(_name) self.logger.setLevel(logging.DEBUG) fh = logging.FileHandler(os.path.join(log_path, now + '.log'), encoding='utf-8') ch = logging.StreamHandler() formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter)self.logger.addHandler(fh)self.logger.addHandler(ch)def get_log(self):"""return logger:return:"""return self.loggerif __name__ == '__main__':logger = Log('lidi').get_log()logger.info('info logger')logger.debug('debug test logger')