python模块详解 logging
打印日志的五个级别:
import logging
logging.debug('test debug')
logging.info('test info')
logging.warning('test warning')# WARNING:root:test warning
logging.error('test error') # ERROR:root:test error
logging.critical('test critical')# CRITICAL:root:test critical
把日志信息写入到文件:
import logging
logging.basicConfig(filename='app.log',level=logging.DEBUG)
logging.debug('test debug')
logging.info('test info')
logging.warning('test warning')
logging.error('test error')
logging.critical('test critical')
app.log
DEBUG:root:test debug
INFO:root:test info
WARNING:root:test warning
ERROR:root:test error
CRITICAL:root:test critical
添加日志添加时间:
import logging
logging.basicConfig(filename='app.log',level=logging.DEBUG,format='%(asctime)s %(message)s',datefmt='%Y/%m/%d %I:%M:%S %p')
#设置写入的文件、默认级别 (小于这个级别的不写入)、输出格式和时间
logging.debug('test debug') # 2017/08/05 05:02:31 PM test debug
logging.info('test info') # 2017/08/05 05:02:31 PM test info
logging.warning('test warning') # 2017/08/05 05:02:31 PM test warning
logging.error('test error') # 2017/08/05 05:02:31 PM test error
logging.critical('test critical') # 2017/08/05 05:02:31 PM test critical
logging.basicConfig函数各参数:
filename: 指定日志文件名
filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'
format: 指定输出的格式和内容,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: 打印日志信息
datefmt: 指定时间格式,同time.strftime()
level: 设置日志级别,默认为logging.WARNING
stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略
将日志同时输出到文件和屏幕
import logging
logger = logging.getLogger('TEST-LOG')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler() #设置屏幕的handler
ch.setLevel(logging.WARNING)
fh = logging.FileHandler('access.log',encoding="utf-8") #设置输出文件的handler
fh.setLevel(logging.ERROR)
#设置两种输出格式
fh_formatter =logging.Formatter('%(asctime)s %(process)d %(filename)s %(levelname)s %(message)s')
ch_formatter =logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Handler和Formatter关联
fh.setFormatter(fh_formatter)
ch.setFormatter(ch_formatter)
#添加到logger
logger.addHandler(fh)
logger.addHandler(ch)
logger.warning('warning')#2017-08-07 15:15:25,693 - TEST-LOG - WARNING - warning
logger.error('error')#2017-08-07 15:15:25,693 - TEST-LOG - ERROR - error
access.log
2017-08-07 15:15:25,693 5380 01.py ERROR error
自动截断
import logging
from logging import handlers
logger = logging.getLogger("TEST") log_file = 'timelog.log'
fh = handlers.RotatingFileHandler(filename=log_file,maxBytes=10,backupCount=3,encoding="utf-8")
#fh = handlers.TimedRotatingFileHandler(filename=log_file,when="S",interval=5,backupCount=3) formatter = logging.Formatter('%(asctime)s %(module)s:%(lineno)d %(message)s') fh.setFormatter(formatter) logger.addHandler(fh) logger.warning('test11')
logger.warning('test12')
logger.warning('test13')
logger.warning('test14')
timelog.log
2017-08-07 15:41:48,742 02:19 test14
timelog.log1
2017-08-07 15:41:48,735 02:18 test13
timelog.log2
2017-08-07 15:41:48,715 02:17 test12
timelog.log3
2017-08-07 15:41:48,712 02:16 test11
其它详细操作:python 的日志logging模块学习
python模块详解 logging的更多相关文章
- python模块详解 | selenium(持续更新中)
目录: 关于selenium Selenium 安装Selenium 安装浏览器驱动 配置环境变量 selenium方法详解 定位元素 元素操作 浏览器操作 鼠标事件 浏览器事件 设置元素等待 多表单 ...
- python模块详解 random os
random模块 常用方法 random.random() 随机产生一个小于1的浮点数 import random print(random.random()) #0.4153761818276826 ...
- python模块详解
什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的代码(.p ...
- python模块详解 sys shutil
sys模块 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的版本信息 sy ...
- python模块详解 | shutil
简介: shutil是python的一个内置模块,提供了许多关于文件和文件集合的高级操作,特别提供文件夹与文件操作.归档操作了支持文件复制和删除的功能. 文件夹与文件操作: copyfileobj(f ...
- 小白的Python之路 day5 python模块详解及import本质
一.定义 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能) 本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test) 包:用来从逻辑上组织模块 ...
- Python 模块详解及import本质
同在当前目录下的模块和包导入 模块定义 本质就是.py结尾的python文件. 用来从逻辑上组织python代码(变量,函数,类,逻辑) 文件名: test.py; 对应的模块名 : test 模块 ...
- Python模块详解以及import本质,获得文件当前路径os.path.abspath,获得文件的父目录os.path.dirname,放到系统变量的第一位sys.path.insert(0,x)
模块介绍 1.定义: 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test) 包:用来从逻 ...
- python模块详解 re
摘自:python中的正则表达式(re模块) 一.简介 正则表达式本身是一种小型的.高度专业化的编程语言,而在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配.正则表达式模式被 ...
随机推荐
- 开源linux远程登录、远程文件管理(ftp)工具
ssh远程登录用 PuTTY.Xshell 5 如果觉得命令行下敲命令管理文件麻烦,就用WinSCP.FileZilla Client(SSH模式),可做到文件上传.下载.改权限等等,很便捷
- 2016 Multi-University Training Contest 10 [HDU 5861] Road (线段树:区间覆盖+单点最大小)
HDU 5861 题意 在n个村庄之间存在n-1段路,令某段路开放一天需要交纳wi的费用,但是每段路只能开放一次,一旦关闭将不再开放.现在给你接下来m天内的计划,在第i天,需要对村庄ai到村庄bi的道 ...
- day31 管道 进程池 数据共享
1. 管道(了解) #创建管道的类: Pipe([duplex]):在进程之间创建一条管道,并返回元组(conn1,conn2),其中conn1,conn2表示管道两端的连接对象,强调一点:必须 ...
- Idea如何设置代码超出长度限制时自动换行
在[File]-->[Settings]-->[Code Sytle]中勾选[Wrap on typing]选项
- getResourceAsStream properties
try (InputStream is = getClass().getResourceAsStream("/test.properties")) { Properties pro ...
- Python练习六十:网页分析,找出里面的正文与链接
网页分析,找出里面的正文与链接 代码如下: from urllib import request from bs4 import BeautifulSoup request = request.url ...
- Problem07 处理字符串
题目:输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. 程序分析:利用while 语句,条件为输入的字符不为'\n'. import java.util.*; public clas ...
- my.梦幻手游_XP
1.http://my.netease.com/thread-459708-1-1.html 2. 3.
- Vue.js-----轻量高效的MVVM框架(十一、使用slot分发内容)
#单个slot html: <h3>#单个slot</h3> <!-- 定义child01模板 --> <template id="child01& ...
- 转 深入解析:一主多备DG环境,failover的实现过程详解 以及 11g 容灾库可以在线添加tempfile. 以及 11g 容灾库可以在线添加logile.
https://yq.aliyun.com/articles/229600 核心,就是11g通过datafille_scn 号来追日志,而不是日志序列号来追日志. 加快standby switchov ...