日誌級別 critical > error > warning > info > debug, 預設是從warning開始列印
import logging# 日誌級別 critical > error > warning > info > debug
配置的 basicconfig檔案只能輸出到檔案中,但是配合stream引數可以達到螢幕/檔案均輸出的效果預設追加模式
預設是輸出到螢幕,有filename則輸出到檔案
直接用logging配置import loggingformat引數中可能用到的格式化串:# 配置的 basicconfig檔案只能輸出到檔案中,但是配合stream引數可以達到螢幕/檔案均輸出的效果
logging.basicconfig(level=logging.debug, ormat='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %y %h:%m:%s',
#stream 輸出的流
filename='test.log', # 預設是輸出到螢幕,有filename則輸出到檔案
filemode='a') # 預設追加模式
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
%(name)s logger的名字logger物件:可以檔案列印,也可以螢幕輸出%(levelno)s 數字形式的日誌級別
%(levelname)s 文字形式的日誌級別
%(pathname)s 呼叫日誌輸出函式的模組的完整路徑名,可能沒有
%(filename)s 呼叫日誌輸出函式的模組的檔名
%(module)s 呼叫日誌輸出函式的模組名
%(funcname)s 呼叫日誌輸出函式的函式名
%(lineno)d 呼叫日誌輸出函式的語句所在的**行
%(created)f 當前時間,用unix標準的表示時間的浮 點數表示
%(relativecreated)d 輸出日誌資訊時的,自logger建立以 來的毫秒數
%(asctime)s 字串形式的當前時間。預設格式是 「2003-07-08 16:49:45,896」。逗號後面的是毫秒
%(thread)d 執行緒id。可能沒有
%(threadname)s 執行緒名。可能沒有
%(process)d 程序id。可能沒有
%(message)s使用者輸出的訊息
import logging# 建立了乙個logger物件,並且命名為
log = logging.logger('user_logger', level=logging.info)
# 更改日誌級別,預設是warning級別
# log.setlevel(logging.debug)
# 檔案輸出物件
log_txt = logging.filehandler('log.log')
# 螢幕輸出物件
log_str = logging.streamhandler()
log_format = logging.formatter('%(asctime)s - %(name)s - %(levelname)s -[line:%(lineno)d]- %(message)s')
# 螢幕輸出
log_str.setformatter(log_format)
# 檔案輸出
log_txt.setformatter(log_format)
# log 物件新增螢幕輸出
log.addhandler(log_str)
# log 物件新增檔案輸出
log.addhandler(log_txt)
# 輸出內容,預設warning以上的log都可以列印
Python學習 重點模組之json
注意 json不能轉換類,不能轉換函式 json.dumps 實現檔案寫入,字串轉換 寫入檔案當然是json字串樓 實際上,json.dumps 只是幫我們做了乙個字串的轉換,把字典轉換為了json格式的字串而已 dict 字典 dict1 json,json.loads dict name 錯誤 ...
python學習之模組
模組與包 1.模組 在 python 中,乙個.py檔案就稱之為乙個模組 module 大大提高了 的可維護性 編寫 不必從零開始。當乙個模組編寫完畢,就可以被其他地方引用 呼叫模組時用import 包名 eg hello模組 def add x,y return x y def jian x,y ...
python模組學習之glob模組
功能描述 glob模組可以使用unix shell風格的萬用字元匹配符合特定格式的檔案和資料夾,跟windows的檔案搜尋功能差不多。glob模組並非呼叫乙個子shell實現搜尋功能,而是在內部呼叫了os.listdir 和fnmatch.fnmatch 檢視我之前寫的fnmatch。glob模組共...