zoukankan      html  css  js  c++  java
  • Python日志模块

    做自动化的时候,我们需要给框架配上日志,出错的时候方便我们查看。

    例子:

      

    import logging
    from Agin_Project.unittest_again.common.file_path import FilePath
    class My_log:
        def conf_log(self,level,msg):
    
            log = logging.getLogger("test_log") #创建一个日志收集器
            log.setLevel(level) #设置日志的级别
    
            # 日志的输出格式
            formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(funcName)s - %(module)s - %(lineno)d : %(message)s')
    
            #输出日志到控制台
            pull = logging.StreamHandler()
            pull.setLevel(level)
            pull.setFormatter(formatter)
    
            #存放日志文件的目录
            file_path = FilePath().file_log_path("log.txt")
            #输出日志到文件
            file_log =logging.FileHandler(file_path,encoding="utf-8")
            file_log.setLevel(level)
            file_log.setFormatter(formatter)
    
            #添加到log里面
            log.addHandler(pull)
            log.addHandler(file_log)
    
            #判断日志的级别
            if level.upper() == "DEBUG":
                log.debug(msg)
    elif level.upper()
    == "INFO": log.info(msg)
    elif level.upper()
    == "WARNING": log.warning(msg)
    elif level.upper()
    == "ERROR": log.error(msg)
    elif level.upper()
    == "CRITICAL": log.critical(msg) #最要要移除日志,不然会有重复的 log.removeHandler(pull) log.removeHandler(file_log) def msg_debug(self,msg): self.conf_log("DEBUG",msg)
    def msg_info(self,msg): self.conf_log(
    "INFO",msg)
    def msg_warning(self,msg): self.conf_log(
    "WARNING",msg)
    def msg_error(self,msg): self.conf_log(
    "ERROR",msg)
    def msg_critical(self,msg): self.conf_log(
    "CRITICAL",msg)
  • 相关阅读:
    div弹出层
    经典SQL语句
    sql连接及操作
    给flash加上连接
    在c#中图片原比例缩放
    悬浮提示筐
    拖动板块
    IFrame自适应高度
    SQL语句中的日期计算
    注意Request.Cookies["UserID"]的用法
  • 原文地址:https://www.cnblogs.com/666666pingzi/p/10372444.html
Copyright © 2011-2022 走看看