Pythong中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='test.log',
                    filemode='a') #可設置a,w(a保存原先的,w重寫覆蓋原先的)
logging.debug('debug message')#Fri,17 Nov 2017 16:20:17 logging_moudle.py [line:8] DEBUG debug message
logging.info('info message')#Fri,17 Nov 2017 16:20:17 logging_moudle.py [line:9] INFO info message
logging.warning('warning message')#Fri,17 Nov 2017 16:20:17 logging_moudle.py [line:10] WARNING warning message
logging.error('error message')#Fri,17 Nov 2017 16:20:17 logging_moudle.py [line:11] ERROR error message
logging.critical('critical message')#Fri,17 Nov 2017 16:20:17 logging_moudle.py [line:12] CRITICAL
 critical message

#日誌模塊
import logging
logger = logging.getLogger() #獲取Logger模塊
logger.setLevel(logging.DEBUG) #設置Logger級別
fh = logging.FileHandler('test2.log')#創建一個handle,用於寫於日誌文件ch = logging.StreamHandler()#再創建一個handle,用於輸出到控制檯formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')#設置格式fh.setFormatter(formatter)ch.setFormatter(formatter)logger.addHandler(fh)logger.addHandler(ch)logger.debug('debug message')logger.info('info message')logger.warning('warning message')logger.error('error message')logger.critical('critical message')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章