Python logging 模块简单实用

来源:互联网 发布:淘宝上买圣衣神话 编辑:程序博客网 时间:2024/06/11 13:09

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=’debuglog.log’,
filemode=’a+’)#定义格式

console = logging.StreamHandler()
console.setLevel(logging.INFO)#定义记录水平
formatter = logging.Formatter(‘%(name)-12s: %(levelname)-8s %(message)s’)
console.setFormatter(formatter)
logging.getLogger(”).addHandler(console)

logging.debug(‘this is debug’) #不被记录 -这个是由console.setLevel(logging.INFO)决定
logging.info(‘this is info’) #不被记录 -这个是由console.setLevel(logging.INFO)决定
logging.warning(‘this is warn’) #被记录-这个是由console.setLevel(logging.INFO)决定
logging.error(‘this is error’) #被记录-这个是由console.setLevel(logging.INFO)决定

0 0
原创粉丝点击