-
Notifications
You must be signed in to change notification settings - Fork 350
/
Copy pathglobal_config.py
57 lines (49 loc) · 2 KB
/
global_config.py
1
# coding:utf-8import osimport loggingfrom logging import handlersos.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'class Logger(object): # https://www.cnblogs.com/nancyzhu/p/8551506.html # 日志级别关系映射 level_relations = { 'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING, 'error': logging.ERROR, 'crit': logging.CRITICAL } def __init__(self, filename, level='info', when='D', backCount=3, fmt='%(asctime)s|%(filename)s[line:%(lineno)d]|%(funcName)s|%(levelname)s|%(message)s'): 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.TimedRotatingFileHandler(filename=filename, when=when, backupCount=backCount, encoding='utf-8') # 实例化TimedRotatingFileHandler # interval是时间间隔,backupCount是备份文件的个数,如果超过这个个数,就会自动删除,when是间隔的时间单位,单位有以下几种: # S 秒 # M 分 # H 小时、 # D 天、 # W 每星期(interval==0时代表星期一) # midnight 每天凌晨 # 设置文件里写入的格式 th.setFormatter(format_str) # 把对象加到logger里 self.logger.addHandler(sh) self.logger.addHandler(th)#loginfo = Logger("recommend_articles.log", "info")if __name__ == '__main__': log = Logger('all.log',level='debug') log.logger.debug('debug') log.logger.info('info') log.logger.warning('警告') log.logger.error('报错') log.logger.critical('严重') Logger('error.log', level='error').logger.error('error')