功能1. 日誌格式的規範
2. 操作的簡化
3. 日誌的分級管理
logging不能幫你做的事情自動生成你要列印的內容
需要程式設計師自己在開發的時候定義好 :
在哪些地方需要列印,要列印的內容是什麼,內容的級別
logging模組的使用 :普通配置型 簡單的 可定製化差
物件配置型 複雜的 可定製化強
一、認識日誌分級
import上面debug\info預設不顯示,但是實在要顯示咋辦?logging
logging.debug(
'debug message
') #
除錯模式 一般不顯示 10
logging.info('
info message
') #
基礎資訊 一般不顯示 20
logging.warning('
warning message
') #
警告 30
logging.error('
error message
') #
錯誤 40
logging.critical('
critical message
')#嚴重錯誤 50
+++++++++++++++++++++++warning:root:warning message
error:root:error message
critical:root:critical message
配置日誌級別
import設定格式,輸出到當前螢幕上logging
logging.basicconfig(level=logging.debug) # 10配置日誌級別logging.debug('
debug message
') #
除錯模式
logging.info('
info message
') #
基礎資訊
logging.warning('
warning message
') #
警告logging.error('
error message
') #
錯誤logging.critical('
critical message
')#嚴重錯誤
import配置輸入到檔案內 新增 filename='test.log'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',
)logging.debug(
'debug message
') #
除錯模式
logging.info('
info message
') #
基礎資訊
logging.warning('
warning message
') #
警告logging.error('
error message
') #
錯誤logging.critical('
critical message
')#嚴重錯誤
********************=wed, 08 apr 2020 22:18:40 8.logging模組.py[line:39] debug debug message
wed, 08 apr 2020 22:18:40 8.logging模組.py[line:40] info info message
wed, 08 apr 2020 22:18:40 8.logging模組.py[line:41] warning warning message
wed, 08 apr 2020 22:18:40 8.logging模組.py[line:42] error error message
wed, 08 apr 2020 22:18:40 8.logging模組.py[line:43] critical critical message
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
')
所以用下面:
二、basicconfig
logger物件的形式來操作日誌檔案
建立乙個logger物件
logger = logging.getlogger()
建立乙個檔案管理操作符
fh = logging.filehandler('logger.log
',encoding='
utf-8
') # 輸出中文
建立乙個螢幕管理操作符
sh = logging.streamhandler()
建立乙個日誌輸出的格式
format1 = logging.formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s
')
例項化的過程。這四個之間沒有任何關係!所以我們:
檔案管理操作符 繫結乙個 格式
fh.setformatter(format1)
螢幕管理操作符 繫結乙個 格式
sh.setformatter(format1)
logger.setlevel(logging.debug) # 配置日誌級別
logger物件 繫結 檔案管理操作符
logger.addhandler(fh)
logger物件 繫結 螢幕管理操作符
logger.addhandler(sh)
logger.debug('view codedebug message
') #
除錯模式
logger.info('
我的資訊
') #
基礎資訊
logger.warning('
warning message
') #
警告logger.error('
error message
') #
錯誤logger.critical('
critical message
')#嚴重錯誤
日誌模組 logging模組
logging.debug 通常除錯時用到的日誌資訊 logging.info 證明事情按照預期的那樣工作 longging.warning 表明發生了意外,或者不就得將來發生的問題 如 磁碟滿了 軟體還是正常的工作 longging.error 由於更嚴重的問題導致軟體已經不能繼續執行某些功能 l...
logging日誌模組
日誌級別日誌輸出 將日誌輸出到控制台 log1.py 如下 import logging logging.basicconfig level logging.warning,format asctime s filename s line lineno d levelname s message s...
logging 日誌模組
什麼是日誌 無處不在的 所有的程式必須記錄日誌 給使用者看的 購物軟體 銀行卡給內部人員看的 給技術人員看的 計算器500個表示式 一些計算過程,或者是一些操作過程需要記錄下來 程式出現bug的時候,來幫助我們記錄過程 排除錯誤 給非技術人員看的 學校,公司的軟體 誰在什麼時候做了什麼事兒,刪除操作...