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库的更多相关文章

  1. Python日志库logging总结-可能是目前为止将logging库总结的最好的一篇文章

    在部署项目时,不可能直接将所有的信息都输出到控制台中,我们可以将这些信息记录到日志文件中,这样不仅方便我们查看程序运行时的情况,也可以在项目出现故障时根据运行时产生的日志快速定位问题出现的位置. 1. ...

  2. 【踩坑记录】记录一次使用Python logging库多进程打印日志的填坑过程

    背景: 项目使用Python自带的logging库来打印日志 项目部署在一台Centos7的机器上 项目采用gunicorn多进程部署 过程: 1.LOG日志代码封装: 采用logging库,并设置w ...

  3. python 记录日志logging

    在项目开发中,往往要记录日志文件.用python记录日志有两种方式: 1.利用python 自带的logging库,例如: # -*- coding: utf-8 -*- import osimpor ...

  4. Python:logging 的巧妙设计

    引言 logging 的基本用法网上很多,这里就不介绍了.在引入正文之前,先来看一个需求: 假设需要将某功能封装成类库供他人使用,如何处理类库中的日志? 数年前在一个 C# 开发的项目中,我用了这样的 ...

  5. python 各种开源库

    测试开发 来源:https://www.jianshu.com/p/ea6f7fb69501 Web UI测试自动化 splinter - web UI测试工具,基于selnium封装. 链接 sel ...

  6. python 三方面库整理

    测试开发 Web UI测试自动化 splinter - web UI测试工具,基于selnium封装. selenium - web UI自动化测试. –推荐 mechanize- Python中有状 ...

  7. Python模块——logging模块

    logging模块简介 logging模块定义的函数和类为应用程序和库的开发实现了一个灵活的事件日志系统.logging模块是Python的一个标准库模块, 由标准库模块提供日志记录API的关键好处是 ...

  8. day27 python学习 logging

    logging模块 函数式简单配置 import logging logging.debug('debug message') logging.info('info message') logging ...

  9. python面试题库——1Python基础篇

    第一部分 Python基础篇(80题) 为什么学习Python? 语言本身简洁,优美,功能超级强大,跨平台,从桌面应用,web开发,自动化测试运维,爬虫,人工智能,大数据处理都能做 Python和Ja ...

随机推荐

  1. springBoot 发送邮件图片不显示

    解决方案 MimeMessageHelper 的执行顺序错了,先执行 setText() 然后执行 addInline() 添加图片 <img src="cid:p03"/& ...

  2. BK: How to read a book 第一篇

    第一章 阅读的活力与艺术 主动阅读 VS 被动阅读(新闻媒体) 作者与读者:投手与捕手的关系. 阅读的目标: 为获得资讯而读,以及为求得理解而读. 为获得咨讯而读,不会增加我们的理解力.比如阅读报纸, ...

  3. js替换从excel复制的文本的换行

    var newStr=oldStr.replace(/\n/g,','); 该代码即可将文本中的换行替换为, oldStr为从excel复制过来的值  newStr为替换后的值

  4. python笔记20(面向对象课程二)

    今日内容 类成员 成员修饰符 内容回顾 & 补充 三大特性 封装 函数封装到类 数据封装到对象 * class Foo: def __init__(self,name,age): self.n ...

  5. JavaScript DOM–节点操作

    节点 节点至少拥有nodeType(节点类型).nodeName(节点名称)和nodeValue(节点值)这三个基本属性. 元素节点 nodeType  为1 属性节点 nodeType  为2 文本 ...

  6. C++-POJ2234-Matches Game[Nim][SG函数]

    #include <set> #include <map> #include <cmath> #include <queue> #include < ...

  7. 洛谷P1170 兔八哥与猎人 欧拉函数的应用

    https://www.luogu.org/problem/P1170 #include<bits/stdc++.h> using namespace std; ],b[],c[],d[] ...

  8. Shashlik Cooking

    Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simu ...

  9. LED Holiday Light -Picking LED Christmas Lights, 4 Things

    LED Christmas lights are not very cheap, but you should know that LED lights can be used for more th ...

  10. AcWing 9. 分组背包问题

    #include <iostream> #include <algorithm> using namespace std; ; int n, m; int v[N][N], w ...