mylogger.py (设置日志输出格式)
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import logging
from logging import Logger
from common.my_path import Outputs_dir
import os
"""
0.logging 默认一个日志收集器root
1.创建一个日志收集器
2.设置日志格式,将控制台日志信息输入到日志文件中
3、以下这些方法:
info()
debug()
error()
warning()
critical()
"""
class myLogger(Logger):
def __init__(self, name, level=logging.INFO,file=None):
# 1.设置日志的名字和收集器
super().__init__(name, level)
# 2.设置日志格式
fmt = "%(asctime)s %(name)s %(levelname)s %(filename)s [%(lineno)d] %(message)s"
fmt_type = logging.Formatter(fmt)
# 3.控制台渠道设置格式
# 控制台渠道StreamHandler
handle1 = logging.StreamHandler()
handle1.setFormatter(fmt_type)
# 将渠道与日志收集器绑定起来
self.addHandler(handle1)
# 4.文件渠道信息设置格式
# 文件渠道FileHandler
if file:
handle2 = logging.FileHandler(file,encoding='utf-8')
handle2.setFormatter(fmt_type)
# 将渠道与日志收集器绑定起来
self.addHandler(handle2)
# 指定保存日志的日志文件名称:路径+文件名
log_path = os.path.join(Outputs_dir, 'AutoTest.log')
logger = myLogger('api-log',logging.INFO,log_path)
# logger.error("打印错误")