测试中的Logging

想要在封装类中使用logger.error等方法,
需要在init方法中self.logger = logger,
再写这些方法

    def debug(self, msg):
        return self.logger.debug(msg)

    def error(self, msg):
        return self.logger.error(msg)

常用方法是继承自这个类
super().init(logger_name)
就可以直接调用

在项目中一般使用一个log文件,在其他文件中想要使用log方法打印log文件
一般在继承类中初始化
logger = LoggerHandle('python3', path_name='log_d1.log')
其他文件中直接导入实例对象
from common.log.log_handle_2 import logger
更便捷,不容易写错代码
以下是logging常用步骤

import logging
logging.debug('这是一个debug')
logging.warning('这是一个警告')
logging.error('出错了')
logging.critical('崩溃了')

'''
日志级别
NOSET 0 
debug 10 调试模式
info 20 详细信息
warning 30 警告
error 40 错误
critical 50 崩溃
'''
import os

logger = logging.getLogger('拍一套honey')
logger.setLevel("DEBUG")

path = os.path.dirname(os.path.abspath('__file__'))
new_path = os.path.join(path,'log_study')
file_path = os.path.join(new_path, 'log.log')

handle = logging.FileHandler(file_path)
logger.addHandler(handle)

format = logging.Formatter('"%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s"')<https://docs.python.org/2/library/logging.html>
handle.setFormatter(format)

logger.error('error')
posted @ 2020-11-02 16:50  一二三开花  阅读(87)  评论(0编辑  收藏  举报