標籤:ogg 模組名 mha 顯示 文本 ctime port stream test
一,日誌模組:logging
1 #簡單應用 2 import logging 3 logging.debug("hello python") 4 logging.info("hello world") 5 logging.warning("hello java") #記錄層級預設WARNUNG 只顯示warning一下的,上面的debug,info沒列印出來 6 logging.error("hello C") 7 logging.critical("hello C++") 8 9 #列印結果:10 #WARNING:root:hello java11 #ERROR:root:hello C12 #CRITICAL:root:hello C++13 #
日誌的等級從下到上:critical-->error-->warning-->info-->debug
2,靈活設定日誌等級,位置,日誌格式
1 import logging 2 #日誌等級設定成debug 3 logging.basicConfig(level=logging.DEBUG, #日誌等級設定成debug 4 format="%(asctime)s %(filename)s %(levelname)s %(message)s", 5 filename = "test.txt", #設定檔案名稱 6 filemode = "w", #以w模式寫入內容 7 ) 8 logging.debug("debug message") 9 logging.info("info message")10 logging.warning("warning message")11 logging.error("error message")12 logging.critical("critical message")
2.1 test.txt檔案中的內容:
2017-04-27 19:29:49,808 logging模組.py DEBUG debug message2017-04-27 19:29:49,825 logging模組.py INFO info message2017-04-27 19:29:49,826 logging模組.py WARNING warning message2017-04-27 19:29:49,826 logging模組.py ERROR error message2017-04-27 19:29:49,826 logging模組.py CRITICAL critical message
2.2 format中可能用到的格式化串
1 ‘‘‘ 2 3 %(name)s Logger的名字 4 %(levelno)s 數字形式的記錄層級 5 %(levelname)s 文本形式的記錄層級 6 %(pathname)s 調用日誌輸出函數的模組的完整路徑名,可能沒有 7 %(filename)s 調用日誌輸出函數的模組的檔案名稱 8 %(module)s 調用日誌輸出函數的模組名 9 %(funcName)s 調用日誌輸出函數的函數名10 %(lineno)d 調用日誌輸出函數的語句所在的程式碼11 %(created)f 目前時間,用UNIX標準的表示時間的浮 點數表示12 %(relativeCreated)d 輸出日誌資訊時的,自Logger建立以 來的毫秒數13 %(asctime)s 字串形式的目前時間。預設格式是 “2003-07-08 16:49:45,896”。逗號後面的是毫秒14 %(thread)d 線程ID。可能沒有15 %(threadName)s 線程名。可能沒有16 %(process)d 進程ID。可能沒有17 %(message)s使用者輸出的訊息18 ‘‘‘
2.3 logger對象方法:推薦用法
1 import logging 2 #日誌模組 3 def get_logger(): 4 logger = logging.getLogger() #獲得一個logger對象 5 6 fh = logging.FileHandler("a.txt") # 建立一個檔案流,需要給一個檔案參數 7 8 sh = logging.StreamHandler() #建立一個螢幕流, 9 10 logger.setLevel(logging.DEBUG) #設定最低等級debug11 #寫入檔案的中的格式12 fm = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")13 14 logger.addHandler(fh)#把檔案流添加進來,流向檔案15 logger.addHandler(sh) #把螢幕流添加進來,流向螢幕16 17 fh.setFormatter(fm) #在檔案流添加寫入格式18 sh.setFormatter(fm) #在螢幕流添加寫入格式19 20 return logger21 22 logger = get_logger()23 logger.debug("hello world")24 logger.info("world python")25 logger.warning("hello java")26 logger.error("python C")27 logger.critical("hello C++")
2.3 a.txt檔案中內容
1 #內容都是一樣的,只是兩個流派都弄出來了 2 3 #螢幕上的內容: 4 2017-04-27 19:57:38,336 - root - DEBUG - hello world 5 2017-04-27 19:57:38,337 - root - INFO - world python 6 2017-04-27 19:57:38,337 - root - WARNING - hello java 7 2017-04-27 19:57:38,337 - root - ERROR - python C 8 2017-04-27 19:57:38,338 - root - CRITICAL - hello C++ 9 10 11 #檔案中的內容:12 2017-04-27 19:57:38,336 - root - DEBUG - hello world13 2017-04-27 19:57:38,337 - root - INFO - world python14 2017-04-27 19:57:38,337 - root - WARNING - hello java15 2017-04-27 19:57:38,337 - root - ERROR - python C16 2017-04-27 19:57:38,338 - root - CRITICAL - hello C++
二:序列化模組:json 重要
python基礎------模組