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模块,程序媛们可以直接调用来实现正则匹配.正则表达式模式被 ...
随机推荐
- 洛谷P3507 [POI2010]GRA-The Minima Game
题目描述 Alice and Bob learned the minima game, which they like very much, recently. The rules of the ga ...
- Qt 学习之路 2(8):添加动作
Home / Qt 学习之路 2 / Qt 学习之路 2(8):添加动作 [在WINDOWS10 QTCREATOR MENU添加无效] Qt 学习之路 2(8):添加动作 豆子 ...
- How to pass multiple parameters in PowerShell invoke-restmethod
Link: http://www.tagwith.com/question_322855_how-to-pass-parameters-in-powershell-invoke-restmethod- ...
- js 多张图片加载 环形进度条
css 部分使用 svg 绘制环形 svg{width:100px; height: 100px; margin:15% auto 25%; box-sizing:border-box; displa ...
- C语言中的头文件
1.头文件#include <> :表示引用标准库头文件,编译器会从系统配置的库环境中去寻找 2.头文件#include "":一般表示用户自己定义使用的头文件,编译器 ...
- 处女座和小姐姐(三)(数位dp)
链接:https://ac.nowcoder.com/acm/contest/329/G 来源:牛客网 题目描述 经过了选号和漫长的等待,处女座终于拿到了给小姐姐定制的手环,小姐姐看到以后直呼666! ...
- 毕业设计 python opencv实现车牌识别 颜色判断
主要代码参考https://blog.csdn.net/wzh191920/article/details/79589506 GitHub:https://github.com/yinghualuow ...
- Java学习笔记day06_自定义类_ArrayList
1.自定义类class 使用类的形式, 对现实中的事物进行描述. 类是引用数据类型. 事物: 方法,变量. 方法:事物具备的功能. 变量:事物的属性. 格式: public class 类名{ //属 ...
- vue中this.$router.push() 传参
1 params 传参 注意⚠️:patams传参 ,路径不能使用path 只能使用name,不然获取不到传的数据 this.$router.push({name: 'dispatch', para ...
- spring AOP注解
此段小代码演示了spring aop中@Around @Before @After三个注解的区别@Before是在所拦截方法执行之前执行一段逻辑.@After 是在所拦截方法执行之后执行一段逻辑.@A ...