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) 将日志输出在控制台

  1. import logging
  2. logging.basicConfig(level=logging.INFO,
  3. filemode='a',
  4. format="[%(levelname)s][%(asctime)s]%(filename)s-%(lineno)d %(message)s")
  5.  
  6. if __name__ == "__main__":
  7.  
  8. logging.debug("this is debug message")
  9. logging.info("this is info message")
  10. logging.warning("this is warning message")
  11. logging.error("this is error message")
  12. logging.critical("this is critical message")

输出结果

  1. [INFO][-- ::,]run.py- this is info message
  2. [WARNING][-- ::,]run.py- this is warning message
  3. [ERROR][-- ::,]run.py- this is error message
  4. [CRITICAL][-- ::,]run.py- this is critical message
  5.  
  6. Process finished with exit code

(demo-2) 将日志输出在日志文件

  1. logging.basicConfig(level=logging.INFO,
  2. filename="%s/run_info.log" % LOG_PATH,
  3. filemode='a',
  4. format="[%(levelname)s][%(asctime)s]%(filename)s-%(lineno)d %(message)s")
  5.  
  6. if __name__ == "__main__":
  7.  
  8. logging.debug("this is debug message")
  9. logging.info("this is info message")
  10. logging.warning("this is warning message")
  11. logging.error("this is error message")
  12. logging.critical("this is critical message")

输出结果

  1. log cat /Users/liurong07/Documents/code/QA/20181018/log/run_info.log
  2. [INFO][2018-10-19 11:07:34,372]run.py-25 this is info message
  3. [WARNING][2018-10-19 11:07:34,373]run.py-26 this is warning message
  4. [ERROR][2018-10-19 11:07:34,373]run.py-27 this is error message
  5. [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. [CQOI2009] 中位数 - 桶

    给出 \(1~n\) 的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是 \(b\).中位数是指把所有元素从小到大排列后,位于中间的数. Solution (这个题为什么会被打上数学标签? ...

  2. detach() 使用和.detach()和.data的区别 、cpu()函数的作用

    detach() 使用和.detach()和.data的区别 .cpu()函数的作用 待办 detach使用 https://blog.csdn.net/qq_27825451/article/det ...

  3. WDatePicker使用 出现ReferenceError: disabledDates is not defined

    "ReferenceError: disabledDates is not defined at eval (eval at <anonymous> at HTMLInputEl ...

  4. Shashlik Cooking

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

  5. 【Python】输出12个星座

    原理:利用Unicode编码 for i in range(12): print(chr(9800+i),end="")

  6. codeforces 1282 E. The Cake Is a Lie (dfs+构造)

    链接:https://codeforces.com/contest/1282/problem/E 题意:给的是一张平面图,是一个n边形,每次可以切一刀,切出一个三角形,最终切成n-2个三角形.题目给出 ...

  7. Python之路【第三十二篇】:django 分页器

    Django的分页器paginator 文件为pageDemo models.py from django.db import models # Create your models here. cl ...

  8. DE1-LINUX运行

    在官网下载.img文件:网址:http://download.terasic.com/downloads/cd-rom/de1-soc/linux_BSP/ 写入DE1_SOC_SD.img文件: 打 ...

  9. Ultra-Thin LED Downlight Selection: 6 Things

    LED Decorative Light Manufacturer    description: ultra-thin LED downlight features can maintain the ...

  10. DuPan不限速教程

    准备: 1.一个百度网盘链接 2.一个可以切换UA的浏览器, 手机版:via,极速浏览器,Kiwi浏览器(推荐)电脑版:未知 3.你的手和脑子