python 自动化测试脚本添加日志

# -*- coding: utf-8 -*-
__author__ = 'mumu'
import logging


class Config(object):
    # 创建一个logger
    logger = logging.getLogger('BB自动化测试')

    if not logger.handlers:
        # 创建一个handler, 用于写入日志文件
        fh = logging.FileHandler('/home/mi/bigbi/test_log/log_file.log')
        fh.setLevel(logging.DEBUG)

        # 再创建一个handler,用于输出到控制台  %(asctime)s - %(name)s - %(levelname)s - %(message)s
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)

        # 定义handler的简单定位错误输出格式(详细格式'%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s')
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)

        # 给logger添加handler
        logger.addHandler(fh)
        logger.addHandler(ch)

    logger.setLevel(logging.DEBUG)

    # logger.setLevel(logging.ERROR)

    def getlog(self):
        return self.logger

可以参照这样的写法

你可能感兴趣的:(log)