Python中logging模块的用法实例

yipeiwu_com6年前Python基础

本文实例讲述了logging模块的用法实例,分享给大家供大家参考。具体方法如下:

import logging 
import os 
 
log = logging.getLogger() 
formatter = logging.Formatter('[%(asctime)s] [%(name)s] %(levelname)s: %(message)s') 
 
 
stream_handler = logging.StreamHandler() 
file_handler = logging.FileHandler(os.path.join("c:\\", "analysis.log")) 
 
 
file_handler.setFormatter(formatter) 
stream_handler.setFormatter(formatter) 
 
 
log.addHandler(file_handler) 
log.addHandler(stream_handler) 
log.setLevel(logging.DEBUG) 
 
 
log.warn("a warning %s " % "c:\\") 

程序运行结果如下:

[2014-09-29 10:23:58,905] [root] WARNING: a warning c:\

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python实现批量下载文件

Python实现批量下载文件 #!/usr/bin/env python # -*- coding:utf-8 -*- from gevent import monkey monk...

Python3中内置类型bytes和str用法及byte和string之间各种编码转换 问题

Python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分。文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示。Python 3不会以任意隐式的...

python经典趣味24点游戏程序设计

python经典趣味24点游戏程序设计

一、游戏玩法介绍: 24点游戏是儿时玩的主要益智类游戏之一,玩法为:从一副扑克中抽取4张牌,对4张牌使用加减乘除中的任何方法,使计算结果为24。例如,2,3,4,6,通过( ( ( 4...

python实现字符串连接的三种方法及其效率、适用场景详解

python字符串连接的方法,一般有以下三种: 方法1:直接通过加号(+)操作符连接 website = 'python' + 'tab' + '.com' 方法2:join方法...

Python编码类型转换方法详解

本文实例讲述了Python编码类型转换方法。分享给大家供大家参考,具体如下: 1:Python和unicode 为了正确处理多语言文本,Python在2.0版后引入了Unicode字符串...