Home  >  Article  >  Backend Development  >  Python中logging模块的用法实例

Python中logging模块的用法实例

WBOY
WBOYOriginal
2016-06-16 08:41:291200browse

本文实例讲述了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程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn