zoukankan      html  css  js  c++  java
  • python

    # -*- coding:utf-8 -*-
    
    '''
    @project: ApiAutoTest
    @author: Jimmy
    @file: get_logger.py
    @ide: PyCharm Community Edition
    @time: 2018-12-29 14:41
    @blog: https://www.cnblogs.com/gotesting/
    
    '''
    
    import logging
    import time
    
    class GetLog:
    
        def __init__(self):
            curTime = time.strftime('%Y-%m-%d')
            self.logname = 'Reports/Log/' + 'AutoTest' + '_' + curTime + '.log'
    
        def get_log(self,level,msg):
    
            # 创建日志收集器
            logger = logging.getLogger()
            logger.setLevel('DEBUG')
    
            # 创建handler
            fh = logging.FileHandler(self.logname,'a',encoding='gbk')
            fh.setLevel('INFO')
            ch = logging.StreamHandler()
            ch.setLevel('INFO')
    
            # 定义handler的输出格式
            formatter = logging.Formatter('%(asctime)s - %(filename)s - %(name)s - %(levelname)s - 日志信息: %(message)s')
            ch.setFormatter(formatter)
            fh.setFormatter(formatter)
    
            # 给logger添加handler
            logger.addHandler(fh)
            logger.addHandler(ch)
    
    
            if level == 'DEBUG':
                logger.debug(msg)
            elif level == 'INFO':
                logger.info(msg)
            elif level == 'WARNING':
                logger.warning(msg)
            elif level == 'ERROR':
                logger.error(msg)
            elif level == 'CRITICAL':
                logger.critical(msg)
    
            logger.removeHandler(fh)
            logger.removeHandler(ch)
            fh.close()
    
        def log_debug(self,msg):
            self.get_log('DEBUG',msg)
    
        def log_info(self,msg):
            self.get_log('INFO',msg)
    
        def log_warning(self,msg):
            self.get_log('WARNING',msg)
    
        def log_error(self,msg):
            self.get_log('ERROR',msg)
    
        def log_critical(self,msg):
            self.get_log('CRITICAL',msg)
  • 相关阅读:
    MongoDB
    新公司管理经营
    剪辑理论
    色彩理论
    商业模式
    电影手法
    [好文转载] 每个程序员都应该学习使用Python或Ruby
    File类递归
    【CF1447】div2复盘
    【心得体会】11.9-11.15
  • 原文地址:https://www.cnblogs.com/gotesting/p/10196119.html
Copyright © 2011-2022 走看看