python模块学习之logging
filename:用指定的文件名创建FiledHandler(后边会具体讲解handler的概念),这样日志会被存储在指定的文件中。
filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。
format:指定handler使用的日志显示格式。
datefmt:指定日期时间格式。
level:设置rootlogger(后边会讲解具体概念)的日志级别
stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件,默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。 format参数中可能用到的格式化串:
%(name)s Logger的名字
%(levelno)s 数字形式的日志级别
%(levelname)s 文本形式的日志级别
%(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
%(filename)s 调用日志输出函数的模块的文件名
%(module)s 调用日志输出函数的模块名
%(funcName)s 调用日志输出函数的函数名
%(lineno)d 调用日志输出函数的语句所在的代码行
%(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
%(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
%(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
%(thread)d 线程ID。可能没有
%(threadName)s 线程名。可能没有
%(process)d 进程ID。可能没有
%(message)s用户输出的消息
例子:
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%Y, %b %d %a %H:%M:%S',
filename='test.log',
filemode='w') logging.debug('1')
logging.info('2')
logging.warning('3')
logging.error('4')
logging.critical('5') 结果:
2017, Jul 19 Wed 11:45:50 e01.py[line:8] DEBUG 1
2017, Jul 19 Wed 11:45:50 e01.py[line:9] INFO 2
2017, Jul 19 Wed 11:45:50 e01.py[line:10] WARNING 3
2017, Jul 19 Wed 11:45:50 e01.py[line:11] ERROR 4
2017, Jul 19 Wed 11:45:50 e01.py[line:12] CRITICAL 5
常见的用法:
logging配置文件
logging.conf
[loggers]
keys=root,simpleExample [handlers]
keys=consoleHandler [formatters]
keys=simpleFormatter [logger_root]
level=DEBUG
handlers=consoleHandler [logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0 [handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,) [formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=
程序实例:
import logging
import logging.config logging.config.fileConfig("logging.conf") # 采用配置文件 # create logger
logger = logging.getLogger("simpleExample") # "application" code
logger.debug("debug message")
logger.info("info message")
logger.warn("warn message")
logger.error("error message")
logger.critical("critical message")
python模块学习之logging的更多相关文章
- Python模块学习:logging 日志记录
原文出处: DarkBull 许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪.在.NET平台中,有非常著名的第三方开源日志组件log4net ...
- 【目录】Python模块学习系列
目录:Python模块学习笔记 1.Python模块学习 - Paramiko - 主机管理 2.Python模块学习 - Fileinput - 读取文件 3.Python模块学习 - Confi ...
- 【转】Python模块学习 - fnmatch & glob
[转]Python模块学习 - fnmatch & glob 介绍 fnmatch 和 glob 模块都是用来做字符串匹配文件名的标准库. fnmatch模块 大部分情况下使用字符串匹配查找特 ...
- 【转】python模块分析之logging日志(四)
[转]python模块分析之logging日志(四) python的logging模块是用来写日志的,是python的标准模块. 系列文章 python模块分析之random(一) python模块分 ...
- python模块分析之logging日志(四)
前言 python的logging模块是用来设置日志的,是python的标准模块. 系列文章 python模块分析之random(一) python模块分析之hashlib加密(二) python模块 ...
- Python模块学习filecmp文件比较
Python模块学习filecmp文件比较 filecmp模块用于比较文件及文件夹的内容,它是一个轻量级的工具,使用非常简单.python标准库还提供了difflib模块用于比较文件的内容.关于dif ...
- Python模块04/包/logging日志
Python模块04/包/logging日志 目录 Python模块04/包/logging日志 内容大纲 1.包 2.logging日志 3.今日总结 内容大纲 1.包 2.logging日志 1. ...
- python基础学习十 logging模块详细使用【转载】
很多程序都有记录日志的需求,并且日志中包含的信息既有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,主要用于输出 ...
- python模块详解 logging
打印日志的五个级别: import logging logging.debug('test debug') logging.info('test info') logging.warning('tes ...
随机推荐
- AtCoder - 2064 Many Easy Problems
Problem Statement One day, Takahashi was given the following problem from Aoki: You are given a tree ...
- React Native 让组件做到局部刷新
利用RN的状态机机制,我们可以通过this.setState({optional:...})来控制界面的刷新,但是一定会触发render方法,那如何保证不调用render方法从而做到界面的局部刷新呢? ...
- U盘安装Ubuntu 16.04出现:Failed to load ldlinux.c32
启动的时候如果不开启UEFI,则会提示: Failed to load ldlinux.c32 Boot failed: please change disks and press a key to ...
- adobe flash builder 4.6最新能用的序列号
神key来了: 1424-4464-3877-6130-5013-5457 妈的,我两台开发机器,有一台死活激活不了,每隔一段时间就去网上搜搜激活方式已经快成为我的一个生活习惯了,可惜每次出来的都是下 ...
- docker常用命令 状态图
http://blog.csdn.net/permike/article/details/51879578
- 【jQuery】:not选择器的说明和:checked选择器的使用
1.:not选择器的说明使用 先给出一下例子: $(".form1 :not(input[name='category'])") 这个 能实现 获取到from1表单中除了input ...
- macOS中Vim基本配置,颜色主题/语法/indent设置
macOS中Vim基本配置 Vim的初始化配置 .vimrc 存放位置 macOS 环境下 vim 的初始化配置文件为 .vimrc , 通常有两个(系统版本和用户版本),一个位于 /usr/shar ...
- [转]Nginx调用远程php-fpm
Nginx调用远程php-fpm 前后端分离的网站,要在异地部署多套网页的时候,这种nginx配置调用远程php-fpm的方式,不错.可以提高网页的相应速度. 原文: https://www.cnbl ...
- Charles 抓HTTPS包报以下错误:
1.You may need to configure your browser or application to trust the Charles Root Certificate. See S ...
- linux ps 命令的结果中VSZ,RSS,STAT的含义和大小
linux ps 命令的结果中VSZ,RSS,STAT的含义和大小 ps是linux系统的进程管理工具,相当于windows中的资源管理器的一部分功能. 一般来说,ps aux命令执行结果的几个列的信 ...