python logging—模块
python logging模块
python logging提供了标准的日志接口,python logging日志分为5个等级:
debug(), info(), warning(), error() and critical()
简单用法
import logging
logging.warning("warning.........")
logging.critical("server is down")
print:
WARNING:root:warning.........
CRITICAL:root:server is down
5个日志级别所代表的意思:
Level | 说明 |
---|---|
DEBUG | Detailed information, typically of interest only when diagnosing problems. |
INFO | Confirmation that things are working as expected. |
WARNING | An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected. |
ERROR | Due to a more serious problem, the software has not been able to perform some function. |
CRITICAL | A serious error, indicating that the program itself may be unable to continue running. |
把日志写到文件里
import logging
logging.basicConfig(filename="log_test.log", level=logging.INFO) #此处定义了日志级别,INFO以及高于INFO的日志会被记录
logging.debug("debug..............")
logging.info("info..............")
logging.warning("warning...........")
结果:
我们把日志级别调整一下:
import logging
logging.basicConfig(filename="log_test.log", level=logging.DEBUG) #level 改为 debug
logging.debug("debug..............")
logging.info("info..............")
logging.warning("warning...........")
结果:
debug 级别的日志已经记录了。另外,日志的写入方式是追加,不会覆盖之前的日志。
自定义日志格式
日志格式参数:
%(name)s | Logger的名字 |
---|---|
%(levelno)s | 数字形式的日志级别 |
%(levelname)s | 文本形式的日志级别 |
%(pathname)s | 调用日志输出函数的模块的完整路径名,可能没有 |
%(filename)s | 调用日志输出函数的模块的文件名 |
%(module)s | 调用日志输出函数的模块名 |
%(funcName)s | 调用日志输出函数的函数名 |
%(lineno)d | 调用日志输出函数的语句所在的代码行 |
%(created)f | 当前时间,用UNIX标准的表示时间的浮 点数表示 |
%(relativeCreated)d | 输出日志信息时的,自Logger创建以 来的毫秒数 |
%(asctime)s | 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒 |
%(thread)d | 线程ID。可能没有 |
%(threadName)s | 线程名。可能没有 |
%(process)d | 进程ID。可能没有 |
%(message)s | 用户输出的消息 |
上段代码看一下:
import logging
logging.basicConfig(filename="log_test.log",
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(module)s - %(lineno)d %(message)s",
datefmt="%Y-%m-%d %I:%M:%S:%p"
)
def fun1():
logging.error("error......")
fun1()
logging.debug("debug..............")
logging.info("info..............")
logging.warning("warning...........")
结果:
高级用法
1.生成logger对象
logger = logging.getLogger("web")
logger.setLevel(logging.INFO) #设置日志级别。默认日志级别为 warning
# 2.生成handler对象
console_handler = logging.StreamHandler() # 用于打印的handler
console_handler.setLevel(logging.WARNING) #也可以专门针对 handler设置日志级别
file_handler = logging.FileHandler("web.log") # 用于输出到文件的 handler
file_handler.setLevel(logging.ERROR)
# 2.1 把handler 对象 绑定到 logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)
# 3.生成formatter 对象
console_formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
file_formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(lineno)s - %(message)s")
# 3.1 把formatter 对象 绑定到 handler
console_handler.setFormatter(console_formatter)
file_handler.setFormatter(file_formatter)
logger.info("info----")
logger.warning("warning-------------")
logger.error("error-------")
全局设置的日志级别 和 handler等设置的日志级别,是逐级过滤的。
比如:
- 全局设置的级别是 info ,handler 设置的级别是 debug , 到最后,debug级别的日志会被过滤掉
- 全局设置的界别是 info,handler设置的级别是 error ,到最后,error已经高于error 级别的日志会被输出
过滤日志:
# *_*coding:utf-8 *_*
import logging
class IngonreBackupLogFilter(logging.Filter):
"""查找带 db backup 的日志"""
def filter(self, record):#固定写法
return "db backup" in record.getMessage()
# 1.生成logger对象
logger = logging.getLogger("web")
logger.setLevel(logging.DEBUG) #设置日志级别。默认日志级别为 warning
# 1.1 把filter对象添加到logger中
logger.addFilter(IngonreBackupLogFilter())
# 2.生成handler对象
console_handler = logging.StreamHandler() # 用于打印的handler
file_handler = logging.FileHandler("web.log") # 用于输出到文件的 handler
# 2.1 把handler 对象 绑定到 logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)
# 3.生成formatter 对象
console_formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
file_formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(lineno)s - %(message)s")
# 3.1 把formatter 对象 绑定到 handler
console_handler.setFormatter(console_formatter)
file_handler.setFormatter(file_formatter)
logger.info("info----")
logger.warning("warning-------------")
logger.error("error-------")
logger.error("error db backup ----")
# 打印:
# 2018-07-08 20:10:40,023 - web - ERROR - error db backup ----
文件自动截断:
import logging
from logging import handlers
logger = logging.getLogger(__name__)
log_file = "timelog.log"
# file_handler = handlers.RotatingFileHandler(filename=log_file, maxBytes=10, backupCount=3)
file_handler = handlers.TimedRotatingFileHandler(filename=log_file,when="S",interval=5,backupCount=3)
formatter = logging.Formatter('%(asctime)s - %(module)s - %(lineno)d - %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.warning("test1")
logger.warning("test2")
logger.warning("test3")
logger.warning("test4")
结果:会按时间生成不同的日志文件:
file_handler = handlers.TimedRotatingFileHandler(filename=log_file,when="S",interval=5,backupCount=3)
You can use the when to specify the type of interval. The list of possible values is below. Note that they are not case sensitive.
(你可以使用when来指定interval的类型。可能的值列表如下。注意它们不是区分大小写的。 )
(这个英语还是有很有必要花时间去搞一下的)
Value | Type of interval |
---|---|
'S' |
Seconds |
'M' |
Minutes |
'H' |
Hours |
'D' |
Days |
'W' |
Week day (0=Monday) |
'midnight' |
Roll over at midnight 午夜roll over |
python logging—模块的更多相关文章
- python logging模块可能会令人困惑的地方
python logging模块主要是python提供的通用日志系统,使用的方法其实挺简单的,这块就不多介绍.下面主要会讲到在使用python logging模块的时候,涉及到多个python文件的调 ...
- python logging模块使用
近来再弄一个小项目,已经到收尾阶段了.希望加入写log机制来增加程序出错后的判断分析.尝试使用了python logging模块. #-*- coding:utf-8 -*- import loggi ...
- 读懂掌握 Python logging 模块源码 (附带一些 example)
搜了一下自己的 Blog 一直缺乏一篇 Python logging 模块的深度使用的文章.其实这个模块非常常用,也有非常多的滥用.所以看看源码来详细记录一篇属于 logging 模块的文章. 整个 ...
- (转)python logging模块
python logging模块 原文:http://www.cnblogs.com/dahu-daqing/p/7040764.html 1 logging模块简介 logging模块是Python ...
- Python logging 模块学习
logging example Level When it's used Numeric value DEBUG Detailed information, typically of interest ...
- Python logging模块无法正常输出日志
废话少说,先上代码 File:logger.conf [formatters] keys=default [formatter_default] format=%(asctime)s - %(name ...
- 0x03 Python logging模块之Formatter格式
目录 logging模块之Formatter格式 Formater对象 日志输出格式化字符串 LogRecoder对象 时间格式化字符串 logging模块之Formatter格式 在记录日志是,日志 ...
- 0x01 Python logging模块
目录 Python logging 模块 前言 logging模块提供的特性 logging模块的设计过程 logger的继承 logger在逻辑上的继承结构 logging.basicConfig( ...
- Python Logging模块的简单使用
前言 日志是非常重要的,最近有接触到这个,所以系统的看一下Python这个模块的用法.本文即为Logging模块的用法简介,主要参考文章为Python官方文档,链接见参考列表. 另外,Python的H ...
随机推荐
- 如何在ThinkPHP中开启调试模式
1.为什么使用调试模式? 因为调试会在我们的模板页的最后增加一些trace信息. 2.什么是trace信息? 是ThinkPHP为我们提供好的一些包含了系统运行时间.占用内存.加载时间.请求的协议.. ...
- springMVC静态资源访问
springMVC默认是访问不到静态资源的,如css,js等,需要在xml里进行配置 保证已经配置好了 web.xml, <!-- Spring MVC servlet --> <s ...
- 用Gvim建立IDE编程环境 (Windows篇)
转自:http://my.oschina.net/kontor/blog/50717 0.准备软件及插件.(a)gvim72.exe 地址ftp://ftp.vim.org/pub/vim/pc/gv ...
- [转](.NET Core C#) AES Encryption
本文转自:https://www.example-code.com/dotnet-core/crypt2_aes.asp Chilkat.Crypt2 crypt = new Chilkat.Cryp ...
- dubbo-admin网页管理控制台
由于近段时间在看dubbo,网上找到的这个war包发布到tomcat报错,故从git(https://github.com/apache/incubator-dubbo-ops)上重新下载编译了版本 ...
- PAT 1037 Magic Coupon
#include <cstdio> #include <cstdlib> #include <vector> #include <algorithm> ...
- PAT 1083 List Grades
#include <cstdio> #include <cstdlib> using namespace std; class Stu { public: ]; ]; }; i ...
- cordova 开发 ios app 简要流程
1 安装node.js环境 官网: http://nodejs.org/ 点击[install],会下载mac的安装包.正常安装即可 2 安装cordova:npm install -g cordo ...
- 我的Android开发之路——ListView的使用
在Android开发过程中,遇到需要列表显示的时候,这时候就会用到listview. 1.首先创建一个ListViewTest项目,选择empty activity类型.修改activity_main ...
- github代码上传教程
github 上传代码步骤 一.git以及Github Git是个正快速成长的版本控制系统,它由GitHub维护. 优势: 1.支持离线开发,离线Repository. 2.强大的分支功能,适合多个独 ...