Python内置模块logging管理不同级别log打印和存储,非常方便,从此告别了使用print打桩记录,我们来看下logging的魅力吧
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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 = './logcheck.log' , filemode = 'w' ) ############################################################################### #define one StreamHandler, set the log mode console = logging.StreamHandler() console.setLevel(logging.INFO) formatter = logging.Formatter( '%(name)-12s: %(levelname)-8s %(message)s' ) console.setFormatter(formatter) logging.getLogger('').addHandler(console) ############################################################################### filePath = r 'C:\ddms.bat' logging.error( 'Open file failed!' ) logging.warn( 'sort mode disabled' ) logging.debug( '%s' % filePath) logging.info( 'xml file generated successfully!' ) |
运行结果:
1
2
3
|
root : ERROR Open file failed! root : WARNING sort mode disabled root : INFO xml file generated successfully! |
总结
以上就是本文关于Python logging管理不同级别log打印和存储实例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
原文链接:http://blog.csdn.net/henryghx/article/details/51039937