码迷,mamicode.com
首页 > 编程语言 > 详细

Python日志模块

时间:2019-02-14 00:24:43      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:自动   path   info   span   div   error   add   模块   elf   

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

例子:

  

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)

 

Python日志模块

标签:自动   path   info   span   div   error   add   模块   elf   

原文地址:https://www.cnblogs.com/666666pingzi/p/10372444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!