python logging模块学习

本文介绍了Python的logging模块,包括日志级别如debug、info、warning、error、critical,以及如何将日志写入文件。讨论了日志输出的格式和配置方法,并提到了官方文档的进阶内容,强调了logger、handler和formatter的使用。学习logging模块有助于在Python+selenium自动化框架中进行日志封装。
摘要由CSDN通过智能技术生成

跟着博主 http://blog.csdn.net/u011541946/article/details/70198676 的文章,在python+selenium 走向自动化框架还需要学会python logging模块,进行日志类的封装,其实不太了解logging模块,所以先了解一下logging模块
一、日志级别:debug、info、warning、error、critical,notset默认级别为warning
日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET

import logging
logging.warning('Watch out!')  # will print a message to the console
logging.info('I told you so')  # will not print anything

上面代码片中warning可打印到控制台中,info不会打印,因为默认的日志级别warning

二、将日志写入文件中

import logging
import os

file_path = os.path.dirname(os.getcwd())
logging.basicConfig(filename=file_path+r'\example.txt',level=logging.DEBUG)
logging.debug("this message should go to the log file")
logging.info('include this')
logging.warning('and this')

#print(os.getcwd())    获得当前路径
#print(os.path.dirname(os.getcwd()))  获得当前路径上一级路径

将日志文件写入文件example.txt,设置日志级别为DBUG,则info、warning级别日志都能写入文件中
三、日志输出格式及方式配置
1、默认输出格式

import  logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
#按照格式日志级别、日志信息格式输出(感觉就像用字典格式化字符串的写法&#x
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值