定义新的logger

def new_logger(name='my_access',log_dir=r'D:\log'):
    logger = logging.getLogger(name)
    logger.propagate = False  # 不传播父logger的消息,避免重复打印
    logger.handlers.clear() # 每次被调用后,清空已经存在的handler,避免重复打印日志
    logger.setLevel(logging.INFO)

    logpath=os.path.join(log_dir(),'log','job_access.log')
    ch=logging.FileHandler(logpath)
    ch.setLevel(logging.INFO)

    # 设置日志格式
    formatter = logging.Formatter("%(asctime)s %(msecs)03d - %(name)s - %(levelname)s - %(lineno)s %(message)s",
                                  datefmt="%Y-%m-%d %H:%M:%S")
    ch.setFormatter(formatter)
    # 将相应的handler添加在logger对象中
    logger.addHandler(ch)

    return logger

  

posted @ 2023-05-08 15:17  垄上行  阅读(34)  评论(0编辑  收藏  举报