• UI自动化之日志的处理


    写自动化时,我们常常希望打印出浏览器的操作记录,也同时希望报错的记录能够保留并用于问题的排查,这时候可以用到loggging模块

    目录

     1、logging文件

    2、调用日志

    1、logging文件

    # coding:utf-8
    import logging, time
    import os
    # log_path是存放日志的路径
    cur_path = os.path.dirname(os.path.realpath(__file__))
    log_path = os.path.join(os.path.dirname(cur_path), 'logs')
    # 如果不存在这个logs文件夹,就自动创建一个
    if not os.path.exists(log_path):os.mkdir(log_path)
     
    class Log():
        def __init__(self):
            # 文件的命名
            self.logname = os.path.join(log_path, '%s.log'%time.strftime('%Y_%m_%d'))
            self.logger = logging.getLogger()
            self.logger.setLevel(logging.DEBUG)
            # 日志输出格式
            self.formatter = logging.Formatter('[%(asctime)s] - %(filename)s] - %(levelname)s: %(message)s')
     
        def __console(self, level, message):
            # 创建一个FileHandler,用于写到本地
            fh = logging.FileHandler(self.logname, 'a', encoding='utf-8')  # 追加模式
            fh.setLevel(logging.DEBUG)
            fh.setFormatter(self.formatter)
            self.logger.addHandler(fh)
     
            # 创建一个StreamHandler,用于输出到控制台
            ch = logging.StreamHandler()
            ch.setLevel(logging.DEBUG)
            ch.setFormatter(self.formatter)
            self.logger.addHandler(ch)
     
            if level == 'info':
                self.logger.info(message)
            elif level == 'debug':
                self.logger.debug(message)
            elif level == 'warning':
                self.logger.warning(message)
            elif level == 'error':
                self.logger.error(message)
            # 这两行代码是为了避免日志输出重复问题
            self.logger.removeHandler(ch)
            self.logger.removeHandler(fh)
            # 关闭打开的文件
            fh.close()
     
        def debug(self, message):
            self.__console('debug', message)
     
        def info(self, message):
            self.__console('info', message)
     
        def warning(self, message):
            self.__console('warning', message)
     
        def error(self, message):
            self.__console('error', message)
     
    if __name__ == "__main__":
       log = Log()
       log.info("---执行步骤----")

      

    2、调用日志

    我们可以在每一步操作成功或者失败时调用这个写日志操作,这样便于问题的排查。一般这种调用日志的操作会写在driver中

  • 相关阅读:
    [Flux] Component / Views
    [Flux] Stores
    [WebStrom] Change default cmd to Cygwin
    [AngularJS] ng-if vs ng-show
    [ES6] Array.find()
    [ES6] Array.findIndex()
    [Javascript] Object.assign()
    [Javascript] Intro to the Web Audio API
    [Falcor] Indroduce to Model
    [Farcol] Introduce
  • 原文地址:https://www.cnblogs.com/weizhideweilai/p/9716108.html
Copyright © 2020-2023  润新知