18Python标准库系列之logging模块
Python标准库系列之logging模块
This module defines functions and classes which implement a flexible event logging system for applications and libraries.
The key benefit of having the logging API provided by a standard library module is that all Python modules can participate in logging, so your application log can include your own messages integrated with messages from third-party modules.
官方文档:https://docs.python.org/3.5/library/logging.html
logging模块用于便捷记录日志且线程安全。
日志级别
Level | Numeric value |
---|---|
CRITICAL | 50 |
ERROR | 40 |
WARNING | 30 |
INFO | 20 |
DEBUG | 10 |
NOTSET | 0 |
只有大于当前日志等级的操作才会被记录。
实例
写入单文件
代码
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# 导入logging模块
import logging# 创建一个log.log日志文件
logging.basicConfig(filename='log.log',# 格式化的字符串format='%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s',# 时间datefmt='%Y-%m-%d %H:%M:%S %p',# 错误级别level=logging.NOTSET)logging.critical('critical')
logging.error('error')
logging.warning('warning')
logging.info('info')
logging.debug('debug')
logging.log(logging.INFO, 'NOTSET')
执行结果
ansheng@ansheng-me:~$ ls
log.py
ansheng@ansheng-me:~$ python log.py
ansheng@ansheng-me:~$ ls
log.log log.py
ansheng@ansheng-me:~$ cat log.log
2016-05-27 21:46:15 PM - root - CRITICAL - log: critical
2016-05-27 21:46:15 PM - root - ERROR - log: error
2016-05-27 21:46:15 PM - root - WARNING - log: warning
2016-05-27 21:46:15 PM - root - INFO - log: info
2016-05-27 21:46:15 PM - root - DEBUG - log: debug
2016-05-27 21:46:15 PM - root - INFO - log: NOTSET
logging.basicConfig函数各参数
参数 | 说明 |
---|---|
filename | 指定日志文件名 |
filemode | 和file函数意义相同,指定日志文件的打开模式,’w’或’a’ |
format | 指定输出的格式和内容,format可以输出很多有用信息,如下所示 |
datefmt | 指定时间格式,同time.strftime() |
level | 设置日志级别,默认为logging.WARNING |
format参数
参数 | 说明 |
---|---|
%(levelno)s | 打印日志级别的数值 |
%(levelname)s | 打印日志级别名称 |
%(pathname)s | 打印当前执行程序的路径,其实就是sys.argv[0] |
%(filename)s | 打印当前执行程序名 |
%(funcName)s | 打印日志的当前函数 |
%(lineno)d | 打印日志的当前行号 |
%(asctime)s | 打印日志的时间 |
%(thread)d | 打印线程ID |
%(threadName)s | 打印线程名称 |
%(process)d | 打印进程ID |
%(message)s | 打印日志信息 |
多文件日志
对于上述记录日志的功能,只能将日志记录在单文件中,如果想要设置多个日志文件,logging.basicConfig将无法完成,需要自定义文件和日志操作对象。
#!/usr/bin/env python
# _*_ coding:utf-8 _*_import logging# 创建文件
file_1 = logging.FileHandler("log1.log", "a")
# 创建写入的日志格式
fmt1 = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s - %(module)s : %(message)s")
# 文件用格式
file_1.setFormatter(fmt1)file_2 = logging.FileHandler("log2.log", "a")
fmt2 = logging.Formatter()
file_2.setFormatter(fmt2)logger1 = logging.Logger("s1", level=logging.ERROR)
logger1.addHandler(file_1)
logger1.addHandler(file_2)logger1.critical("1111")
# 定义文件
file_2_1 = logging.FileHandler('l2_1.log', 'a')
fmt = logging.Formatter()
file_2_1.setFormatter(fmt)# 定义日志
logger2 = logging.Logger('s2', level=logging.INFO)
logger2.addHandler(file_2_1)
如上述创建的两个日志对象
当使用
logger1
写日志时,会将相应的内容写入 l1_1.log 和 l1_2.log 文件中当使用
logger2
写日志时,会将相应的内容写入 l2_1.log 文件中
#Python标准库 #Logging
转载于:https://blog.51cto.com/edeny/1925799
18Python标准库系列之logging模块相关推荐
- Python标准库中的logging模块
1.将日志输出到屏幕 import logging logging.debug('This is debug message') logging.info('This is ...
- python解析xml文件选用模块_Python标准库系列之xml模块
Python's interfaces for processing XML are grouped in the xml package. 带分隔符的文件仅有两维的数据:行和列.如果你想在程序之间交 ...
- Python全栈之路标准库系列之random模块
This module implements pseudo-random number generators for various distributions. random.random() 生成 ...
- python的threading库_python标准库介绍——31 threading 模块详解
threading 模块 (可选) ``threading`` 模块为线程提供了一个高级接口, 如 [Example 3-1 #eg-3-1] 所示. 它源自 Java 的线程实现. 和低级的 ``t ...
- 细数python标准库中低调的模块
有没有遇到过这种情况,在网络上搜索如何使用Python进行某种操作,最终找到一个第三方库,直到后来发现标准库中包含的模块或多或少都可以满足你的需求.这种情况并不罕见, 整理了一些python标准库中鲜 ...
- Python标准库datetime之date模块详解
Python标准库datetime之date模块详解 datetime是Python提供的操作日期和时间的标准库,主要有datetime.date模块.datetime.time模块及datetime ...
- Python标准库笔记(9) — functools模块
functools 作用于函数的函数 functools 模块提供用于调整或扩展函数和其他可调用对象的工具,而无需完全重写它们. 装饰器 partial 类是 functools 模块提供的主要工具, ...
- Python标准库datetime之datetime模块详解
Python标准库datetime之datetime模块详解 1.日期时间对象 日期时间对象是指具有日期(年月日)和时间(时分秒)双重属性的实例 日期时间对象的类型为datetime.datetime ...
- Python标准库中的os模块
Python的标准库中的os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux ...
最新文章
- ros udp协议53端口 封_每天一点IT-说说AD域的端口
- 设计模式 — 结构型模式 — 外观模式
- Java基础班学习笔记(15)GUI图形界面编程
- The DVMM Lab at Columbia University
- 聚类算法 sklearn k_means (返回一维数据的最优聚类)
- flask mysql 版本_Flask mysql
- 设备树(device tree)学习笔记
- zend framework1.12 没找到php.exe,请加一个zend framework的有关问题
- kafka消息消费有延迟_RabbitMQ与Kafka的技术差异以及使用注意点
- c# oldb连接_如何使用C#中的OleDbConnection 连接读取Excel?
- 学习理论:理论联系实际--演绎归纳演绎
- 江苏2021高考成绩查询全省排名,江苏省高考排名对应大学-江苏考生位次查询(2021年参考)...
- Authorization loop detected on Conduit with realm “WSMAN“
- 深度学习基础--Bottleneck(瓶颈) Architectures
- 《SQL基础》04. SQL-DQL
- 记一次面试准备(续上)
- linux php-fpm配置,php-fpm配置
- MySQL8免安装版下载安装与配置(windows)
- 第12集 副词及其他
- 河南单招计算机分数线,2017年河南高职院校单招录取分数线