python的logging库
logging库
简介
logging库提供日志打印功能。
值得一提的是,不仅能打印到日志文件,还能打印到控制台。
日志级别
logging一共分为5个级别,从低到高依次为: DEBUG<INFO<WARNING<ERROR<CRITICAL
日志参数配置
配置接口logging.basicConf()
参数详解
控制日志的级别
level=logging.DEBUG (或者: logging.INFO / logging.WARNING / logging.ERROR / logging.CRITICAL)
假如level=logging.WARNING, 那么低于warning级别的日志都不会打印了。
设置日志名字(也设置了日志存放的路径)
filename="%s/test.log" % "/home/work"
设置文件模式
什么是文件模式呢?
filemode='w' (或者'a')
'w'表示什么意思?
'a'表示什么意思?
设置日志格式
控制了每一行日志都输出哪些字段
format="%(levelname)s-%(asctime)s-%(filename)s-%(funcName)s-%(lineno)d-%(message)s"
其中每个字段什么意思呢,可以参考下面
日志级别
%(levelno)s 日志级别数值
%(levelname)s 日志级别名字
%(asctime)s 日志打印时间
%(filename)s 文件名称
%(funcName)s 函数名称
%(lineno)d 行号
%(process)d 进程ID
%(thread)d 线程ID
%(threadName) 线程名称
%(message)s 打印日志信息
demo
(demo-1) 将日志输出在控制台
import logging
logging.basicConfig(level=logging.INFO,
filemode='a',
format="[%(levelname)s][%(asctime)s]%(filename)s-%(lineno)d %(message)s") if __name__ == "__main__": logging.debug("this is debug message")
logging.info("this is info message")
logging.warning("this is warning message")
logging.error("this is error message")
logging.critical("this is critical message")
输出结果
[INFO][-- ::,]run.py- this is info message
[WARNING][-- ::,]run.py- this is warning message
[ERROR][-- ::,]run.py- this is error message
[CRITICAL][-- ::,]run.py- this is critical message Process finished with exit code
(demo-2) 将日志输出在日志文件
logging.basicConfig(level=logging.INFO,
filename="%s/run_info.log" % LOG_PATH,
filemode='a',
format="[%(levelname)s][%(asctime)s]%(filename)s-%(lineno)d %(message)s") if __name__ == "__main__": logging.debug("this is debug message")
logging.info("this is info message")
logging.warning("this is warning message")
logging.error("this is error message")
logging.critical("this is critical message")
输出结果
➜ log cat /Users/liurong07/Documents/code/QA/20181018/log/run_info.log
[INFO][2018-10-19 11:07:34,372]run.py-25 this is info message
[WARNING][2018-10-19 11:07:34,373]run.py-26 this is warning message
[ERROR][2018-10-19 11:07:34,373]run.py-27 this is error message
[CRITICAL][2018-10-19 11:07:34,374]run.py-28 this is critical message
python的logging库的更多相关文章
- Python日志库logging总结-可能是目前为止将logging库总结的最好的一篇文章
在部署项目时,不可能直接将所有的信息都输出到控制台中,我们可以将这些信息记录到日志文件中,这样不仅方便我们查看程序运行时的情况,也可以在项目出现故障时根据运行时产生的日志快速定位问题出现的位置. 1. ...
- 【踩坑记录】记录一次使用Python logging库多进程打印日志的填坑过程
背景: 项目使用Python自带的logging库来打印日志 项目部署在一台Centos7的机器上 项目采用gunicorn多进程部署 过程: 1.LOG日志代码封装: 采用logging库,并设置w ...
- python 记录日志logging
在项目开发中,往往要记录日志文件.用python记录日志有两种方式: 1.利用python 自带的logging库,例如: # -*- coding: utf-8 -*- import osimpor ...
- Python:logging 的巧妙设计
引言 logging 的基本用法网上很多,这里就不介绍了.在引入正文之前,先来看一个需求: 假设需要将某功能封装成类库供他人使用,如何处理类库中的日志? 数年前在一个 C# 开发的项目中,我用了这样的 ...
- python 各种开源库
测试开发 来源:https://www.jianshu.com/p/ea6f7fb69501 Web UI测试自动化 splinter - web UI测试工具,基于selnium封装. 链接 sel ...
- python 三方面库整理
测试开发 Web UI测试自动化 splinter - web UI测试工具,基于selnium封装. selenium - web UI自动化测试. –推荐 mechanize- Python中有状 ...
- Python模块——logging模块
logging模块简介 logging模块定义的函数和类为应用程序和库的开发实现了一个灵活的事件日志系统.logging模块是Python的一个标准库模块, 由标准库模块提供日志记录API的关键好处是 ...
- day27 python学习 logging
logging模块 函数式简单配置 import logging logging.debug('debug message') logging.info('info message') logging ...
- python面试题库——1Python基础篇
第一部分 Python基础篇(80题) 为什么学习Python? 语言本身简洁,优美,功能超级强大,跨平台,从桌面应用,web开发,自动化测试运维,爬虫,人工智能,大数据处理都能做 Python和Ja ...
随机推荐
- C语言-基本数据类型
一.C语言数据类型深度剖析 1.什么是数据类型? -可以理解为固定内存大小的别名 -数据类型是创建变量的模子 -数据类型是对内存的格式化操作 上面三句话如何理解? 在以前的汇编语言时代,我们要存储一些 ...
- linux - python:卸载
[root@test ~]# rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps ##强制删除已安装程序及其关联[root@test ~]# ...
- 根据ID选中
var name = document.getElementsById("mainStack");
- 模块一:shell 脚本基础
一.shell脚本介绍 (一)脚本案例及介绍: #!/bin/bash LOG_DIR=/var/log ROOT_UID=0 if ["$UID -ne "$ROOT_UID&q ...
- python:if else 语句
#!/usr/bin/python# -*- coding:utf-8 -*- import os fileName1 = 'a.txt'if os.path.exists(fileName1): f ...
- TI DaVinci(达芬奇)入门
(转载来自 德州仪器半导体技术(上海)有限公司 通用DSP 技术应用工程师 崔晶 德州仪器(TI)的第一颗达芬奇(DaVinci)芯片(处理器)DM6446已经问世快三年了.继DM644x之后,TI又 ...
- codeforces 1283F. DIY Garland(树+优先队列)
题目连接:https://codeforces.com/contest/1283/problem/F 题意:一根电线连接着两个点,这两个点分别代表着两个灯,灯有自己的编号i,其亮度是2 ^ i,每根电 ...
- Python_递归函数
楔子 在讲今天的内容之前,我们先来讲一个故事,讲的什么呢?从前有座山,山里有座庙,庙里有个老和尚讲故事,讲的什么呢?从前有座山,山里有座庙,庙里有个老和尚讲故事,讲的什么呢?从前有座山,山里有座庙,庙 ...
- sql server和eclipse连接问题
最近学习java,需要用的数据库sql sever,这就有一个连接问题需要设置 首先需要下载sql server,可查看我的博客sql sever下载教程: 连接教程:eclipse连接sql ser ...
- MYSQL之路之表