同时输出到前台和文件(滚动覆盖)
import os import logging import time from logging.handlers import RotatingFileHandler def login(mm): log = logging.getLogger() log.setLevel(logging.NOTSET) rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time())) log_path = os.path.dirname(os.getcwd()) + '/Logs/' log_name = log_path + 'log.log' logfile = log_name # fsc = logging.FileHandler(logfile, mode='w') # 不循环回滚日志 fsc = RotatingFileHandler(logfile, maxBytes=1, backupCount=2) # 循环回滚日志,最大1kb,最多保留2个备份 fsc.setLevel(logging.NOTSET) ch = logging.StreamHandler() # 创建输出到前台的handler ch.setLevel(logging.NOTSET) # 定义日志显示级别为无级别 formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s:%(message)s") fsc.setFormatter(formatter) ch.setFormatter(formatter) log.addHandler(fsc) log.addHandler(ch) log.debug(mm) # log.info('this is a logger info message') # log.warning('this is a logger warning message') # log.error('this is a logger error message') # log.critical('this is a logger critical message')
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)