Python內(nèi)置模塊logging管理不同級別log打印和存儲,非常方便,從此告別了使用print打樁記錄,我們來看下logging的魅力吧
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!') 運行結(jié)果:
root : ERROR Open file failed! root : WARNING sort mode disabled root : INFO xml file generated successfully!
總結(jié)
以上就是本文關(guān)于Python logging管理不同級別log打印和存儲實例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
新聞熱點
疑難解答