python学习之 logging包
1,logging包
python的一个包,专门用来写日志的。
官方一共划分了6个等级的log类型,分别对应重要性等级50,40,30,20,10,0:
级别排序:CRITICAL > ERROR > WARNING > INFO > DEBUG >NOTSET
2,logging打印日志到控制台(和print效果差不多)
废话不说,直接看代码:
#!coding=utf-8
import logging logging.basicConfig(level=logging.DEBUG, datefmt='%Y/%m/%d %H:%M:%S',format='%(asctime)s : %(name)s : %(levelname)s : %(message)s')
logger = logging.getLogger("daqing") #把设置读取到实例中才能用哦 logging.debug('this is the debug message')
logging.info('this is the info message')
logging.warning('this is the warning message')
logging.error('this is the error message')
logging.critical('this is the critical message')
#返回的结果:
2019/05/30 13:21:46 : root : DEBUG : this is the debug message
2019/05/30 13:21:46 : root : INFO : this is the info message
2019/05/30 13:21:46 : root : WARNING : this is the warning message
2019/05/30 13:21:46 : root : ERROR : this is the error message
2019/05/30 13:21:46 : root : CRITICAL : this is the critical message
比较重要的是bascConfig函数,它必须在一开始就进行定义,它的几个参数如下
# level用于指定最低等级的logging输出,高于或者等于这个等级自动输出,
# format是指定了字符串格式:包括 asctime、name、levelname、message四个内容(还有别的),分别代表运行时间、模块名称、日志级别、日志内容。
#datefomt是用于格式化时间
#filename指定日志文件的名字,这个例子没有带
#filemode "w"表示清空并且写入,“a"表示追加,一般和上一个参数一块用
#style 用于指定format的占位符,必须在format前定义,只能是%,{或者$
3,打印日志到文档
#!coding=utf-8
import logging logging.basicConfig(level=logging.DEBUG,
filename='output.log',
datefmt='%Y/%m/%d %H:%M:%S',
format='%(asctime)s : %(name)s : %(module)s : %(process)d : %(message)s')
logger = logging.getLogger("daqing") #此处logger和logging还是有区别的,logger写出的日志显示的name一项是daqing,而logging显示的name是root
logger.debug('this is the debug message')
logger.info('this is the info message')
logger.warning('this is the warning message')
logging.error('this is the error message')
logging.critical('this is the critical message')
#定义的format是这样的:%(asctime)s : %(name)s : %(module)s : %(process)d : %(message)s
#返回到output.log中的内容:
2019/05/30 13:23:18 : root : logtest : 4928 : this is the debug message
2019/05/30 13:23:18 : root : logtest : 4928 : this is the info message
2019/05/30 13:23:18 : root : logtest : 4928 : this is the warning message
2019/05/30 13:23:18 : root : logtest : 4928 : this is the error message
2019/05/30 13:23:18 : root : logtest : 4928 : this is the critical message
比较重要的是:format内容(常用):
#%(levelname)s:打印日志级别的名称
#%(pathname)s:打印当前执行程序的路径
#%(funcName)s:打印日志的当前函数
#%(lineno)d:打印日志的当前行号
#%(process)d:打印进程ID
#%(message)s:打印日志信息
#%(module)s:打印模块名称
#%(name)s: 用户名称
4,同时把日志打印到控制台和日志文件中
#!ccoding=utf-8
import logging # 第一步,创建一个logger
logger = logging.getLogger("daqing")
logger.setLevel(logging.DEBUG) # Log等级总开关 # 第二步,创建一个handler,用于写入日志文件,用的是 logging.FileHandler函数,注意它的参数信息
logfile = './logger.txt'
fh = logging.FileHandler(logfile,encoding="utf-8", mode='w') #mode="a"则是追加
fh.setLevel(logging.INFO) # 输出到file的log等级的开关 # 第三步,再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG) # 输出到console的log等级的开关 # 第四步,定义handler的输出格式,控制台和输出到文件的handler可以共用
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
ch.setFormatter(formatter) # 第五步,将logger添加到handler里面,这一步是最重要的,本质上就是为logger添加多个handler
logger.addHandler(fh)
logger.addHandler(ch) # 日志
logger.debug('this is a logger debug message')
logger.info('this is a logger info message')
logger.warning('this is a logger warning message')
logger.error('this is a logger error message')
logger.critical('this is
#控制台返回的结果是这样的:
2019-05-30 13:31:46,235 - logtest.py[line:70] - DEBUG: this is a logger debug message
2019-05-30 13:31:46,236 - logtest.py[line:71] - INFO: this is a logger info message
2019-05-30 13:31:46,236 - logtest.py[line:72] - WARNING: this is a logger warning message
2019-05-30 13:31:46,236 - logtest.py[line:73] - ERROR: this is a logger error message
2019-05-30 13:31:46,236 - logtest.py[line:74] - CRITICAL: this is a logger critical message #logger.txt返回的是这样的:
2019-05-30 13:31:46,236 - logtest.py[line:71] - INFO: this is a logger info message
2019-05-30 13:31:46,236 - logtest.py[line:72] - WARNING: this is a logger warning message
2019-05-30 13:31:46,236 - logtest.py[line:73] - ERROR: this is a logger error message
2019-05-30 13:31:46,236 - logtest.py[line:74] - CRITICAL: this is a logger critical message
#两者虽然共用一个format设置,但是level不同。于是。。。
5,捕获解释器返回的异常信息
#!coding=utf-8
import logging logging.basicConfig(level=logging.DEBUG, datefmt='%Y/%m/%d %H:%M:%S',format='%(asctime)s : %(name)s : %(levelname)s : %(message)s')
logger = logging.getLogger("daqing") #把设置读取到实例中才能用哦 logger.info("start")
try:
a=10/0
except Exception:
logger.error("hehe",exc_info=True) #此处打印完“hehe”日志以后,会把try中报错的信息也打印出来
logger.info("finish")
#返回:
2019/05/30 13:42:32 : daqing : INFO : start
2019/05/30 13:42:32 : daqing : ERROR : hehe
Traceback (most recent call last):
File "D:/python_test/logtest/logtest.py", line 88, in <module>
a=10/0
ZeroDivisionError: division by zero
2019/05/30 13:42:32 : daqing : INFO : finish
6,logger是可以继承的
logging.basicConfig(level=logging.DEBUG, datefmt='%Y/%m/%d %H:%M:%S',format='%(asctime)s : %(name)s : %(levelname)s : %(message)s')
logger = logging.getLogger("daqing") #把设置读取到实例中才能用哦 logger.debug('this is a logger debug message')
logger.info('this is a logger info message')
logger.warning('this is a logger warning message')
logger.error('this is a logger error message')
logger.critical('this is a logger critical message') child_logger=logging.getLogger("daqing.hehe") #此处的child_logger继承了名字叫daqing的logger的属性
child_logger.setLevel(level=logging.WARNING) #继承以后修改了部分属性,把level等级改掉了 child_logger.debug('this is a logger debug message')
child_logger.info('this is a logger info message')
child_logger.warning('this is a logger warning message')
child_logger.error('this is a logger error message')
child_logger.critical('this is a logger critical message')
#返回的logger
2019/05/30 13:50:17 : daqing : DEBUG : this is a logger debug message
2019/05/30 13:50:17 : daqing : INFO : this is a logger info message
2019/05/30 13:50:17 : daqing : WARNING : this is a logger warning message
2019/05/30 13:50:17 : daqing : ERROR : this is a logger error message
2019/05/30 13:50:17 : daqing : CRITICAL : this is a logger critical message
#返回的child_logger
2019/05/30 13:50:17 : daqing.hehe : WARNING : this is a logger warning message
2019/05/30 13:50:17 : daqing.hehe : ERROR : this is a logger error message
2019/05/30 13:50:17 : daqing.hehe : CRITICAL : this is a logger critical message
python学习之 logging包的更多相关文章
- python中利用logging包进行日志记录时的logging.level设置选择
之前在用python自带的logging包进行日志输出的时候发现有些logging语句没有输出,感到比较奇怪就去查了一下logging文档.然后发现其在设置和引用时的logging level会影响最 ...
- python学习——模块和包
在之前常用模块中我们已经初步了解了模块的导入,今天来说学习一下模块和包.我们可以把模块理解成每一个python文件.而包就是多个能解决一类问题的python文件全部放在一起.OK
- python学习之logging
学习地址:http://blog.csdn.net/zyz511919766/article/details/25136485 首先如果我们想简要的打印出日志,可以: import logging l ...
- python 学习笔记 -logging模块(日志)
模块级函数 logging.getLogger([name]):返回一个logger对象,如果没有指定名字将返回root loggerlogging.debug().logging.info().lo ...
- python学习日记(包——package)
简述——包 包是一种通过使用‘.模块名’来组织python模块名称空间的方式. 注意: 1. 无论是import形式还是from...import形式,凡是在导入语句中(而不是在使用时)遇到带点的,都 ...
- python学习之模块&包的引用
名词解释: 模块:一个程序文件 包:相当于一个类库,打包发布后相当于c#中的dll, 包中可包括若干个模块,比如main.py就是一个模块,对于test2文件下的所有模块组成一个包 对于一个包而言,注 ...
- python学习-模块与包(九)
9.4查看模块内容 dir(): 返回模块或类所包含的全部程序单元(包括变量.函数.类和方法等) __all__:模块本身提供的变量,不会展示以下划线开头的程序单元.另使用from xx import ...
- python学习笔记013——包package
1 包(模块包)package 1.1 包的定义 包是将模块以文件夹的组织形式进行分组管理的方法 1.2 作用 分类管理,有利于防止命名冲突 可以在需要时加载一个或部分模块,而不是全部模块 mypac ...
- python学习之logging模块
Logger.setLevel(level) 设置记录器的级别为level.低于该级别的信息将被忽略. 记录器默认级别为NOTSET.如果记录器是根记录器,则默认将记录所有信息: 如果是一个非根记录器 ...
随机推荐
- C# extract img url from web content then download the img
static void Main(string[] args) { WebClientDemo(); Console.ReadLine(); } static void WebClientDemo() ...
- 另外一种获取redis cluster主从关系和slot分布的方法
条条大路通罗马,通过最近学习redis cluster 观察其输出,发现了另外一种获取master-slave关系的方法. [redis@lxd-vm1 ~]$ cat get_master_slav ...
- git配置多仓库
git配置多仓库 github , gitee , coding , gitlab , gitlab.company ..... 真TM多 . 真TM多 . 真TM多 . 生成ssh 生成ssh 密钥 ...
- 为data中的某一个对象添加一个属性不起作用——this.$set的正确使用
this.$set(obj, key, value) 我们在项目开发的过程中,经常会遇到这种情况:为data中的某一个对象添加一个属性 <template> <div class=& ...
- python3练习100题——035
原题链接:http://www.runoob.com/python/python-exercise-example34.html 题目:文本颜色设置. 学习了一下python3 的文本颜色设置. 其实 ...
- SpringBoot学习- 6、MetaData描述无法tip显示问题
SpringBoot学习足迹 (一)先说说现象 1.在application.properties可以定义属性并在MetaData中增加description 如 生成additional-sprin ...
- Spring与RestHighLevelClient
Elasticsearch连接方式有两种:分别为TCP协议与HTTP协议 最近使用es比较多,之前使用一直是使用spring封装的spring-data-elasticsearch:关于spring- ...
- Redis到底是多线程还是单线程?线程安全吗,还需要加锁吗?
0. redis单线程问题 单线程指的是网络请求模块使用了一个线程(所以不需考虑并发安全性),即一个线程处理所有网络请求,其他模块仍用了多个线程. 1. 为什么说redis能够快速执行 (1) 绝大部 ...
- ASP.NET Razor简介
Razor 不是一种编程语言.它是服务器端的标记语言. 什么是 Razor? Razor 是一种标记语法,可以让您将基于服务器的代码(Visual Basic 和 C#)嵌入到网页中. 基于服务器的代 ...
- [SDOI2016] 生成魔咒 - 后缀数组,平衡树,STL,时间倒流
[SDOI2016] 生成魔咒 Description 初态串为空,每次在末尾追加一个字符,动态维护本质不同的子串数. Solution 考虑时间倒流,并将串反转,则变为每次从开头删掉一个字符,即每次 ...