python自定义封装logging模块
#coding:utf-8
import logging class TestLog(object):
'''
封装后的logging
'''
def __init__(self , logger = None):
'''
指定保存日志的文件路径,日志级别,以及调用文件
将日志存入到指定的文件中
''' # 创建一个logger
self.logger = logging.getLogger(logger)
self.logger.setLevel(logging.DEBUG)
# 创建一个handler,用于写入日志文件
self.log_time = time.strftime("%Y_%m_%d_")
self.log_path = "D:\\python\\workspace\\pythontest\\log\\"
self.log_name = self.log_path + self.log_time + 'test.log' fh = logging.FileHandler(self.log_name, 'a') # 追加模式 这个是python2的
# fh = logging.FileHandler(self.log_name, 'a', encoding='utf-8') # 这个是python3的
fh.setLevel(logging.INFO) # 再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.INFO) # 定义handler的输出格式
formatter = logging.Formatter('[%(asctime)s] %(filename)s->%(funcName)s line:%(lineno)d [%(levelname)s]%(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter) # 给logger添加handler
self.logger.addHandler(fh)
self.logger.addHandler(ch) # 添加下面一句,在记录日志之后移除句柄
# self.logger.removeHandler(ch)
# self.logger.removeHandler(fh)
# 关闭打开的文件
fh.close()
ch.close() def getlog(self):
return self.logger
封装后的logging代码中format()中的自定义日志格式,可以根据喜好更换:
封装python自带的logging类,向Logger类中传用例名称,用法
log = Logger().getlog() #放在class上面
class ClassName()
log.info("log message")
结果:
[2018-01-17 22:45:05,447] test_mainrun.py test_run_mail line:31 [INFO]截图保存成功,全路径
具体用法:
代码如下:
#coding:utf-8 from selenium import webdriver
import unittest
from pythontest.commlib.baselib import TestLog
#自定义公共模块 log = TestLog().getlog()
class testcals(unittest.TestCase):
u'''【调用】'''
def setUp(self):
self.driver = webdriver.Firefox()
self.base = Screen(self.driver) # 实例化自定义类commlib.baselib def login(self):
url_login = "http://www.baidu.com"
self.driver.get(url_login) def test_01_run_mail(self):
try:
self.login() log.info(self.img)
except Exception as msg:
log.error("异常原因 [ %s ]" % msg) log.error(self.img)
raise def test_02_case(self):
u'''【test_case】'''
log.error("首页error 日志")
log.debug("订单页debug 日志")
log.info("活动页info 日志")
log.critical("支付critical 日志") def tearDown(self):
self.driver.quit() if __name__ == "__main__":
unittest.main()
python自定义封装logging模块的更多相关文章
- Python自建logging模块
本章将介绍Python内建模块:日志模块,更多内容请从参考:Python学习指南 简单使用 最开始,我们用最短的代码体验一下logging的基本功能. import logging logger = ...
- Python实战之logging模块使用详解
用Python写代码的时候,在想看的地方写个print xx 就能在控制台上显示打印信息,这样子就能知道它是什么了,但是当我需要看大量的地方或者在一个文件中查看的时候,这时候print就不大方便了,所 ...
- Python日志输出——logging模块
Python日志输出——logging模块 标签: loggingpythonimportmodulelog4j 2012-03-06 00:18 31605人阅读 评论(8) 收藏 举报 分类: P ...
- Python中的logging模块就这么用
Python中的logging模块就这么用 1.日志日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRITICALDEBUG:详细的信息,通常只出现在诊断问题 ...
- python中日志logging模块的性能及多进程详解
python中日志logging模块的性能及多进程详解 使用Python来写后台任务时,时常需要使用输出日志来记录程序运行的状态,并在发生错误时将错误的详细信息保存下来,以别调试和分析.Python的 ...
- Python 中 对logging 模块进行封装,记录bug日志、日志等级
是程序产生的日志 程序员自定义设置的 收集器和渠道级别那个高就以那个级别输出 日志和报告的作用: 报告的重点在于执行结果(执行成功失败,多少用例覆盖),返回结果 日志的重点在执行过程当中,异常点,哪里 ...
- python日志记录-logging模块
1.logging模块日志级别 使用logging模块简单示例: >>>import logging >>>logging.debug("this's a ...
- Python自动化之logging模块
Logging模块构成 主要分为四个部分: Loggers:提供应用程序直接使用的接口 Handlers:将Loggers产生的日志传到指定位置 Filters:对输出日志进行过滤 Formatter ...
- python笔记7 logging模块 hashlib模块 异常处理 datetime模块 shutil模块 xml模块(了解)
logging模块 日志就是记录一些信息,方便查询或者辅助开发 记录文件,显示屏幕 低配日志, 只能写入文件或者屏幕输出 屏幕输出 import logging logging.debug('调试模式 ...
随机推荐
- [kuangbin带你飞]专题二十二 区间DP-B-LightOJ - 1422
题意大概是这样,第i天必须穿a[i](某一种类)的衣服,你可以套着穿很多件,对于第i天,你有两种操作,一种是脱掉现在的衣服,一种是穿上新的一件,但是你脱掉的衣服,以后不能再穿.问最少需要多少件衣服? ...
- C#如何使SQLite程序集既能适应32位系统也能适应64位系统
分享5: 需求:都知道Sqlite3是分32位和64位版本的,那如果将一个Sqlite3.dll文件全适用 分析:Sqlite是种轻量级的数据库文件,使用了混合编程而成的,一部分采用非托管的C++代码 ...
- 算法01 C语言设计
8.21 #include <stdio.h> void bubbleSort(int **p, int n); int main(void){ int a[100]; int *b[10 ...
- MR-join连接1......
MR-join连接
- php框架之phalcon
1.开发助手 1) 下载 git clone https://github.com/phalcon/cphalcon.git git clone https://github.com/phalcon/ ...
- 原生js,从面向过程的方法到面向对象的方法,写个选项卡练练手
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Linux路径与Win路径的转换
cygpath $ cygpath -p "$WinPath" -u LinuxPath $ cygpath -p "$LinuxPath" -w WinPat ...
- codeforces-1142 (div1)
div1真难,现在就是后悔, 非常后悔 A.显然如果我们知道起点和终点是哪两个点,我们可以算出距离通过b / gcd(a,b)的方式求出需要走几步的. 并且数据范围似乎也允许我们这么做,所以直接枚举取 ...
- HBase LSM树存储引擎详解
1.前提 讲LSM树之前,需要提下三种基本的存储引擎,这样才能清楚LSM树的由来: 哈希存储引擎. B树存储引擎. LSM树(Log-Structured Merge Tree)存储引擎. 2. 哈希 ...
- c语言变量及输入输出
scanf: 格式字符串的一般形式:%[*][输入数据宽度][长度] 类型 (其中有方括号[] 的项为任选项.) 各项意义: 1) 类型:表示输入数据的类型,其格式符和意义如下表所示. ...