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语言】输出半径1到10的圆的面积,当面积值超过100时,停止执行本程序
#include<stdio.h> #define PI 3.142 int main() { int r; float area; ; r <= ; r++) { area = P ...
- 机器学习作业(二)逻辑回归——Python(numpy)实现
题目太长啦!文档下载[传送门] 第1题 简述:实现逻辑回归. 此处使用了minimize函数代替Matlab的fminunc函数,参考了该博客[传送门]. import numpy as np imp ...
- Spring Boot的Web配置
一.使用YML文件配置多套环境
- linux - mysql 异常:MySQL Daemon failed to start.
报错内容 MySQL Daemon failed to start. 如果直接输入 mysql -root -p 登陆会出现 [mysql]ERROR 2002 (HY000): Can't conn ...
- Linux系统搭建Java环境【JDK、Tomcat、MySQL】一篇就够
前言:所有项目在完成开发后都会部署上线的,一般都是用Linux系统作为服务器的,很少使用Windows Server(大多数项目的开发都是在Windows桌面系统完成的),一般有专门负责上线的人员 ...
- idea的插件库连不上网络
如果你试遍了网上的方法都没有解决网络问题,建议换个网络,比如切换到电信网络.
- c++踩坑大法好 数组
1,c++遍历数组 int数组和char数组不同哦,int占4位,char占1未,同理double也不同.基本遍历方法: ] = { ,,, }; ]); printf("len of my ...
- Centos7更改网卡名称Eth0
标签: Centos7更改网卡名称 2016-12-06 21:55 8737人阅读 评论(1) 收藏 举报 分类: linux(6) 作者同类文章X 版权声明:本文为博主原创文章,未经博主允许不 ...
- python:删除文件及文件夹
#!/usr/bin/python# -*- coding:utf-8 -*- import os import shutil os.remove(path) #删除文件shutil.rmtree(p ...
- cat 显示文本、less 分屏显示文本、more 分页显示文件、head 显示文件的前面的内容、cut 切割、paste合并、wc用来对文本进行统计、sort排序、权限、关闭文件、vim的使用
cat 显示文本 -E 显示结尾的$符 -n 对显示的每一行进行编号 -b 对非空行进行编号 -s 对连续的空行进行压缩 tac 倒序显示 less 分屏显示文本 向下翻一屏 空格 向下翻一行 回车 ...