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. IDEA please configure web facet first

  2. Oracle 中的 Incarnation 到底是个什么?实验操作篇

    对于“化身”Incarnation概念了解之后,本篇通过手工恢复实验来具体操作演示,加深对Incarnation的理解,来自于博客园AskScuti. 你可以点击此处查看<概念理解篇>. ...

  3. BindingException: Invalid bound statement (not found)问题

  4. JS代码的位置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Spring整合MyBatis案例练习笔记

    需求: 用户登录 技术需求: Servlet+Spring+Mybatis+MVC+jsp+css+html+jquery 数据库设计: 用户表 Sql语句设计: select * from t_us ...

  6. ASP.NET MVC 用户权限-1

    MVC框架的开发网站的利器,MVC框架也开始越来越流行了.对于.NET ,微软也发布了MVC框架,做网站通常要涉及到用户的权限管理,对于.NET MVC 框架的用户权限管理又应该怎样设置呢?下面通过示 ...

  7. 《NVM-Express-1_4-2019.06.10-Ratified》学习笔记(8.8)-- Reservations

    8.8 Reservations 预订 NVMe的reservation预订功能,用于让两个或多个主机能够协调配合的访问共享namespace.使用这些功能的协议和方式超出了本规格说明书的范围.对这些 ...

  8. laydate 限制结束日期不能大于起始日期

    时间选择器在选择的时候,同时配置了另一个时间选择器内的参数 <div class="form-group"> <label for="exampleIn ...

  9. 显示目录文件命令 - ls

    1) 命令名称:ls 2) 英文原意:list 3) 命令所在路径:/bin/ls 4) 执行权限:所有用户 5) 功能描述:显示目录文件 6) 语法: ls 选项[-ald][文件或目录] -a 显 ...

  10. c数据结构 -- 使用链表实现计数

    #include <stdio.h> #include <stdlib.h> typedef struct _node{ int value; struct _node *ne ...