这篇文章主要为大家展示了“Python中logging管理不同级别log打印和存储的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Python中logging管理不同级别log打印和存储的示例分析”这篇文章吧。
示例
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!')
运行结果:
root : ERROR Open file failed!
root : WARNING sort mode disabled
root : INFO xml file generated successfully!
以上是“Python中logging管理不同级别log打印和存储的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注天达云行业资讯频道!