[python 学习] logging模块
1、将简单日志打印到屏幕:
import logging
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')

默认情况下,logging将日志打印到屏幕,日志级别为WARNING;
日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET
2、设置日志级别:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import logging logging.basicConfig(level=logging.DEBUG)
#日志级别和对应的数字
number_log = [logging.NOTSET,logging.DEBUG,logging.INFO,logging.WARNING,logging.CRITICAL]
print number_log
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')

3、通过logging.basicConfig函数对日志的输出格式及方式做相关配置:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import logging logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='myapp.log',
filemode='w')
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
# filename: 指定日志文件名
# filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'
# format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:
# %(levelno)s: 打印日志级别的数值
# %(levelname)s: 打印日志级别名称
# %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
# %(filename)s: 打印当前执行程序名
# %(funcName)s: 打印日志的当前函数
# %(lineno)d: 打印日志的当前行号
# %(asctime)s: 打印日志的时间
# %(thread)d: 打印线程ID
# %(threadName)s: 打印线程名称
# %(process)d: 打印进程ID
# %(message)s: 打印日志信息
# datefmt: 指定时间格式,同time.strftime()
# level: 设置日志级别,默认为logging.WARNING
# stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,
# 当stream和filename同时指定时,stream被忽略
日志输出到myapp.log:

4、logger实例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import logging #使用logging.getLogger(name)获取一个logger对象
#logging.getLogger()对象是root
logger_root = logging.getLogger()
print logger_root.name #root
print logger_root.__class__ #<class 'logging.RootLogger'> #下面创建两个对象a和b
#b的父对象是a, a的父对象是root
logger_b = logging.getLogger('a.b')
print logger_b.name #a.b
print logger_b.__class__ #<class 'logging.Logger'>
5、logger实例继承:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import logging #使用logging.getLogger(name)获取一个logger对象
#logging.getLogger()对象是root
logger_root = logging.getLogger()
print logger_root.name #root
print logger_root.__class__ #<class 'logging.RootLogger'> #下面创建两个对象a和b
#b的父对象是a, a的父对象是root
logger_b = logging.getLogger('a.b')
print logger_b.name #a.b
print logger_b.__class__ #<class 'logging.Logger'>
6、addHandler:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import logging # 1、创建一个logger
logger =logging.getLogger('root.test')
logger.setLevel(logging.DEBUG) # 2、1 创建一个handler,用于写入日志文件
file_handler = logging.FileHandler('test.log')
file_handler.setLevel(logging.DEBUG) # 3、2 创建一个handler, 用于输出到控制台
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.CRITICAL) #3、定义handler的输出格式
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') #4、给handler添加formatter
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter) #5、给logger添加handler
logger.addHandler(console_handler)
logger.addHandler(file_handler) #控制台将不会输出,日志文件将会被写入
logger.warning('warning');
出处:http://www.cnblogs.com/deeper/p/7404190.html
[python 学习] logging模块的更多相关文章
- Python学习---重点模块的学习【all】
time [时间模块] import time # print(help(time)) # time模块的帮助 print(time.time()) # 时间戳 print(time.cloc ...
- Python之logging模块
一.引言 之前在写一些小程序的时候想把日志内容打到文件中,所以就自己写了一个logger.py的程序,如下: #!/usr/bin/python # -*- coding=utf-8 -*- impo ...
- python的logging模块
python提供了一个日志处理的模块,那就是logging 导入logging模块使用以下命令: import logging logging模块的用法: 1.简单的将日志打印到屏幕上 import ...
- python的logging模块之读取yaml配置文件。
python的logging模块是用来记录应用程序的日志的.关于logging模块的介绍,我这里不赘述,请参见其他资料.这里主要讲讲如何来读取yaml配置文件进行定制化的日志输出. python要读取 ...
- python中logging模块的用法
很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...
- Python学习--Selenium模块
1. Python学习--Selenium模块介绍(1) 2.Python学习--Selenium模块学习(2) 其他: 1. Python学习--打码平台
- Python学习--Selenium模块学习(2)
Selenium的基本操作 获取浏览器驱动寻找方式 1. 通过手动指定浏览器驱动路径2. 通过 `$PATH`环境变量找寻浏览器驱动 可参考Python学习--Selenium模块简单介绍(1) 控制 ...
- day27 python学习 logging
logging模块 函数式简单配置 import logging logging.debug('debug message') logging.info('info message') logging ...
- python基础--logging模块
很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...
随机推荐
- 使用Chrome逆向分析JS实战---分析google网站翻译器原文存放位置
剧透:就是使用了一下Chrome DevTools的Memory功能,通过已知的JS变量的值查找JS内存中变量的引用 一:不分析一下现有的网页翻译方法么? 总所周知,(As is well known ...
- Swiper轮播隐藏再显示后不动
公告用Swiper轮播方式,在某些不需要显示公告的页面进行隐藏,在需要展示公告的页面进行显示时候,公告不能正常轮播,在条件里加入重新设置轮播方法等网上的一些方法仍然不行,最后解决方法: this.my ...
- window.open弹窗阻止问题解决之道
https://segmentfault.com/a/1190000015381923https://segmentfault.com/a/1190000014988094https://www.cn ...
- 【ABAP系列】SAP ALV 导出报表数据 始终使用选定的格式”,一旦勾上,就再也不会弹出选择框了。
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ALV 导出报表数据 始 ...
- 【HANA系列】SAP HANA快捷键大全
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA快捷键大全 ...
- unity编辑器Hierarchy添加图标
效果 素材 using UnityEditor; using UnityEngine; using System.Collections.Generic; [InitializeOnLoad] cla ...
- .net日志的用法
public class Logs { private static Logger logger = LogManager.GetCurrentClassLogger(); //初始化日志类 /// ...
- win10创建扩展分区
1.开始菜单中选择命令提示符,以管理员身份运行. 2.运行“diskpart”命令. 3.DISKPART>后面输入list disk命令,显示磁盘列表. 4.选择磁盘,select disk ...
- poj-2289.jamies contact groups(二分答案 + 二分多重匹配)
Jamie's Contact Groups Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9227 Accepted: ...
- Linux快速访问多个目录
Linux下实现多个目录之间快速切换 dirs -v # 显示栈目录dirs -c # 清空栈目录 pushd # 加入当前目录pushd director # 加入指定目录pushd +/-i ...