python 日誌模組

2022-04-20 09:52:53 字數 1342 閱讀 4300

在軟體或者系統發生錯誤時可以通過日誌快速定位到錯誤,從而定位問題,解決問題。

logging模組提供的日誌記錄函式所使用的日誌器設定的日誌級別是warning,因此只有warning級別的日誌記錄以及大於它的errorcritical級別的日誌記錄被輸出了,而小於它的debuginfo級別的日誌記錄被丟棄了。

>>>:logging.warning('warning bug')

輸出:warning:root:warning bug

日誌級別:日誌器名稱:日誌內容

該方法用於為logging日誌系統做一些基本配置

sys.stdout     標準輸出

sys.stderr      標準錯誤

filename、stream和handlers這三個配置項只能有乙個存在,不能同時出現2個或3個,否則會引發valueerror異常。

import

logging

#logging.basicconfig(level=logging.debug) #指定日誌級別

#log_format = "%(asctime)s - %(levelname)s - %(message)s" # 指定發生時間,級別名字,日誌資訊

#logging.basicconfig(filename='log.log',level=logging.debug,format=log_format)

log_format = "

%(asctime)s - %(levelname)s - %(message)s

"date_format = "

%y/%m/%d %h:%m:%s %p

"logging.basicconfig(filename='

log.log

', level=logging.debug, format=log_format, datefmt=date_format)

logging.debug(

'debug log')

logging.warning(

'warning bug

')

07/23/2018 21:51:32 pm - debug -debug log

07/23/2018 21:51:32 pm - warning -warning bug

2018/07/23 21:52:54 pm - debug -debug log

2018/07/23 21:52:54 pm - warning - warning bug

python日誌模組

logging.debug 10 logging.info 20 logging.warning 30 logging.error 40 logging.critical 50預設級別為warning 預設輸出位置為控制台 import logging logging.basicconfig 可用引...

python日誌模組

python的日誌模組使用logging,如果想要輸出符合自己的預期,需要重新定義,廢話不多說,直接貼 encoding utf 8 from logging.handlers import timedrotatingfilehandler import logging import osimpor...

python 日誌模組

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 ...