Python模块之 - logging
日志是非常重要的,最近有接触到这个,所以系统的看一下Python这个模块的用法。本文即为Logging模块的用法简介,主要参考文章为Python官方文档,链接见参考列表。
Logging模块构成
组成
主要分为四个部分:
- Loggers:提供应用程序直接使用的接口
- Handlers:将Loggers产生的日志传到指定位置
- Filters:对输出日志进行过滤
- Formatters:控制输出格式
日志级别
| Level | Numeric value | When it’s used |
|---|---|---|
| DEBUG | 10 | Detailed information, typically of interest only when diagnosing problems. |
| INFO | 20 | Confirmation that things are working as expected. |
| WARNING | 30 | 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 | 40 | Due to a more serious problem, the software has not been able to perform some function. |
| CRITICAL | 50 | A serious error, indicating that the program itself may be unable to continue running. |
| NOSET | 0 | getattr(logging, loglevel.upper()) |
默认级别是WARNING,可以使用打印到屏幕上的方式记录,也可以记录到文件中。
使用示例
下面的代码展示了logging最基本的用法。
# import logging
#
# logging.basicConfig(filename="log.txt",
# level=logging.DEBUG,
# format="%(asctime)s %(message)s",
# datefmt="%Y-%m")
# logging.debug("SS") """
%(name)s Logger的名字
%(levelno)s 数字形式的日志级别
%(levename)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 class IgnoreBackupLogFilter(logging.Filter):
"""过滤带db backup 的日志""" def filter(self, record): # 固定写法
# true 不记录
return "db backup" not in record.getMessage() # 1、生成logger对象
logger = logging.getLogger("web")
logger.setLevel(logging.DEBUG) # 默认是warning
# 1、1 把filter对象添加到logger中
logger.addFilter(IgnoreBackupLogFilter()) # 2、生成handler对象
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# fh = logging.FileHandler("web.log") # 写到那个文件下
from logging import handlers
fh = handlers.TimedRotatingFileHandler("web.log",when="S",interval=5,backupCount=3)
fh.setLevel(logging.WARNING)
# 2.1 把handler对象 绑定到logger
logger.addHandler(ch)
logger.addHandler(fh) # 3、生成formatter对象
console_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(filename)s - %(message)s ")
file_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(lineno)d - %(filename)s - %(message)s ")
# 3、1 把formatter对象 绑定到handler对象
ch.setFormatter(console_formatter)
fh.setFormatter(file_formatter) logger.debug("s")
logger.info("d")
logger.warning("g")
logger.error("a324")
logger.info("db backup 2314") # globl debug
# console info
# file warning # 全局设置为DEBUG后,console handler 设置为INFO,如果输出的日志级别是debug,那就不输出 """
日志自动截断
"""
# 按文件大小截断
# from logging import handlers
# fh = handlers.RotatingFileHandler("web.log", maxBytes=100, backupCount=13) # 文件最大100个字节,最多保留3个文件,时间最早的删除
# 按时间长短截断
# from logging import handlers
# fh = handlers.TimedRotatingFileHandler("web.log",when="S",interval=5,backupCount=3)
Python模块之 - logging的更多相关文章
- python 模块之-logging
python 模块logging import logging ### 简单使用格式 日志级别等级CRITICAL > ERROR > WARNING > INFO > ...
- python初步学习-python模块之 logging
logging 许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪.在python中,我们不需要第三方的日志组件,python为我们提供了简单易用.且 ...
- Python模块学习 ---- logging 日志记录
许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪.在.NET平台中,有非常著名的第三方开源日志组件log4net,c++中,有人们熟悉的log4cp ...
- Python 模块之Logging——常用handlers的使用
一.StreamHandler 流handler——包含在logging模块中的三个handler之一. 能够将日志信息输出到sys.stdout, sys.stderr 或者类文件对象(更确切点,就 ...
- python模块之logging
在现实生活中,记录日志非常重要.银行转账时会有转账记录:飞机飞行过程中,会有黑盒子(飞行数据记录器)记录飞行过程中的一切.如果有出现什么问题,人们可以通过日志数据来搞清楚到底发生了什么.对于系统开发. ...
- 【python模块】——logging
python学习——logging模块
- Python模块:logging、
logging模块: 很多程序都有记录日志的需求,并且日志中包含的信息既有正常的程序访问日志,还可能有错误.警告等信息输出.Python的logging模块提供了标准的日志接口,你可以通过它存储各种格 ...
- python模块学习 logging
1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('This is info messa ...
- python模块:logging
# Copyright 2001-2016 by Vinay Sajip. All Rights Reserved. # # Permission to use, copy, modify, and ...
随机推荐
- Python3 的描述符--完整例子详细解释
##描述符类的例子,这个例子说明了描述符和被描述符之间的关系 ##摄氏温度 class Celsius(): ## 1 描述符类 def __init__(self,value = 26.0): ## ...
- 使用ADO.NET查询和访问数据库
使用ADO.NET查询和访问数据库步骤 使用ADO.NET查询和访问数据库 连接数据库操作: 1. 定义连接字符串: String connString = "Data Sour ...
- 下一个ajax异步请求被挂起问题
异步请求按理来说应该是会不受其它ajax请求影响的,但如果是服务端访问了Session就不能这么说了. 了解了asp.net的会话管理,那我们来看看今天要谈到的主题: IReadOnlySession ...
- Java基础学习笔记十一 Eclipse开发工具
Eclipse是功能强大Java集成开发工具.它可以极大地提升我们的开发效率.可以自动编译,检查错误.在公司中,使用的就是Eclipse进行开发. Eclipse的下载.安装.卸载 下载 http:/ ...
- 【Nginx系列】Nginx编译与安装
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器.Nginx是由Igor Sysoev为俄罗斯访问第二的Rambler.ru站点开发的. 一.Nginx ...
- Linux下硬盘分区
1 fdisk -l查看硬盘及分区信息 我的系统(Archlinux)下的命令效果如下: 由上面的图片可以得知该系统只挂载了1个硬盘,命名为sda,其有2个主分区,sda1和sda2,至于为什么这么 ...
- django的FBV和CBV
title: python djano CBV FBV tags: python, djano, CBV, FBV grammar_cjkRuby: true --- python django的fu ...
- 释义Oracle 11r2中并行执行相关参数
因最近对现场某些服务器进行诊断和调整,用到了这类参数,因此对这类参数做了详尽的查阅和研究,现将该类参数释义如下,以方便同行和自己参考,禁止转载: 1.PARALLEL_ADAPTIVE_MULTI_U ...
- JS 上传图片时实现预览
网页中一张图片可以这样显示: <img src="http://www.letuknowit.com/images/wg.png"/>也可以这样显示:<img s ...
- docker实践4
我的docker学习笔记4-守护式容器 $docker run -i -t ubuntu /bin/bash $ctrl-p 或 ctrl-q # 转到后台 $docker ps $docke ...