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

Python中logging模块的用法实例

WBOY
Release: 2016-06-16 08:41:29
Original
1225 people have browsed it

本文实例讲述了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:\\") 

Copy after login

程序运行结果如下:

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

Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template