logging模塊是Python的內置模塊,主要用於輸出運行日誌,能夠靈活配置輸出日誌的各項信息。python
logging.basicConfig(level=logging.DEBUG, format='levelname:%(levelname)s filename: %(filename)s ' 'outputNumber: [%(lineno)d] thread: %(threadName)s output msg: %(message)s' ' - %(asctime)s', datefmt='[%d/%b/%Y %H:%M:%S]', filename='./loggmsg.log', filemode="a")
日誌一共分紅5個等級,從低到高分別是:DEBUG ,INFO ,WARNING ,ERROR, CRITICAL。程序員
%(levelno)s: 打印日誌級別的數值 %(levelname)s: 打印日誌級別名稱 %(pathname)s: 打印當前執行程序的路徑,其實就是sys.argv[0] %(filename)s: 打印當前執行程序名 %(funcName)s: 打印日誌的當前函數 %(lineno)d: 打印日誌的當前行號 %(asctime)s: 打印日誌的時間 %(thread)d: 打印線程ID %(threadName)s: 打印線程名稱 %(process)d: 打印進程ID %(message)s: 打印日誌信息
logging.debug('This is debug message') logging.info('This is info message') logging.warning('This is warning message')
import logging logging.basicConfig(level=logging.DEBUG, format='levelname:%(levelname)s filename: %(filename)s ' 'outputNumber: [%(lineno)d] thread: %(threadName)s output msg: %(message)s' ' - %(asctime)s', datefmt='[%d/%b/%Y %H:%M:%S]', filename='./loggmsg.log', filemode="a") logging.debug("Hello")
日誌文件loggmsg.log編程
levelname:DEBUG filename: test.py outputNumber: [7] thread: MainThread output msg: Hello - [19/Nov/2019 22:28:13]
> 想進一步瞭解編程開發相關知識,與我一同成長進步,請關注個人公衆號「松果倉庫」,共同分享宅&程序員的各種資源,謝謝!!!函數