day23-logging模塊

# logging日誌記錄的兩個內容:一、有5種級別的日誌記錄模式。二、兩種配置方式:basicconfig、logger對象。
# logging的做用:
#一、排錯的時候須要打印不少細節來幫助排錯。
#二、把錯誤記錄下來。
#三、用戶行爲,有沒錯都要記錄下來。

# 1. 5種級別的日誌記錄模式:
import logging
logging.debug('debug message') #排錯信息  低級別
logging.info('info message')   #正常信息
logging.warning('warning message') #警告信息
logging.error('error message')  #錯誤信息
logging.critical('critical message') #嚴重錯誤信息  高級別
#WARNING:root:warning message 只記錄了warning,error,critical,由於debug和info級別比較低。
#ERROR:root:error message
#CRITICAL:root:critical message

# 2. 配置方式:basicConfig
import logging
logging.basicConfig(level=logging.DEBUG, #從debug開始記錄。此處DEBUG必須是大寫。
    format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S %p', #時間格式,能夠重設asctime
    filename='x1.log', #文件名
    filemode='a') #文件操做模式是追加。

logging.debug('debug message')#2019-07-31 14:43:46 PM - root - DEBUG -day23-loggingģ��:  debug message
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')

# 3. 用戶行爲(例如input),有沒有錯都要記錄下來。
# 注意:basicConfig沒法讓中文輸出到屏幕,輸出到屏幕的都是亂碼,只能寫到文件裏。
import logging
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S %p',
    filename='x2.log',
    filemode='a',
    level=logging.DEBUG)
try:                       #當try下面的代碼爲False,也就是輸入一個不是int類型的字符串,才執行except下面的代碼。
    int(input('>>>'))
except:
    logging.debug('輸入的不是一個數字')
    #2019-07-31 14:25:14 PM - root - DEBUG -day23-loggingģ��:  ����IJ���һ������
    #當input一個字母,它不是數字,就會執行except下面的代碼,執行結果是屏幕有亂碼,不能打印出'輸入的不是一個數字'到屏幕
    # 由於是中文,若是是英文就沒問題。由於pycharm是uft-8編碼,
    #而寫入到電腦的文件x2.log是gbk編碼,在電腦找到x2.log,打開它,不會有亂碼。
    #若是不把記錄的信息寫入文件,那麼把filename='x2.log',filemode='a'刪除便可,這樣輸出到屏幕就不會有亂碼。

# 4. basicConfig的特色:不能同時往屏幕和文件輸出,只能二選一。

# 5. logger對象配置:默認是'a'追加模式,我發現只能記錄warning以上級別的模式(warning,error,critical)。
# 5.1 輸出到文件log.log:
import logging
logger = logging.getLogger() #logger對象
fh = logging.FileHandler('log.log',encoding = 'utf-8') #文件操做符,用於寫入日誌文件
formatter = logging.Formatter('%(asctime)s,%(module)s,%(message)s') #格式
fh.setFormatter(formatter) #文件操做符 關聯 格式
logger.addHandler(fh) #logger對象 關聯 文件操做符
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')

# 5.2 同時輸出到文件和屏幕控制檯:
import logging
logger = logging.getLogger()

fh = logging.FileHandler('log.log',encoding = 'utf-8')
sh = logging.StreamHandler() #屏幕操做符

formatter = logging.Formatter('%(asctime)s,%(module)s,%(message)s')
formatter1 = logging.Formatter('%(asctime)s,%(message)s')#格式

fh.setFormatter(formatter)
sh.setFormatter(formatter1) #屏幕操做符 關聯 formatter或formatter1均可以。

logger.addHandler(fh)
logger.addHandler(sh)#logger對象 關聯 屏幕操做符

logging.warning('警告信息')
logging.error('錯誤信息')
logging.critical('嚴重錯誤信息')

# 6. 配置參數:
# logging.basicConfig()函數中可經過具體參數來更改logging模塊默認行爲,可用參數有:
# 
# filename:用指定的文件名建立FiledHandler,這樣日誌會被存儲在指定的文件中。
# filemode:文件打開方式,在指定了filename時使用這個參數,默認值爲「a」還可指定爲「w」。
# format:指定handler使用的日誌顯示格式。
# datefmt:指定日期時間格式。
# level:設置rootlogger(後邊會講解具體概念)的日誌級別
# stream:用指定的stream建立StreamHandler。能夠指定輸出到sys.stderr,sys.stdout或者文件(f=open(‘test.log’,’w’)),默認爲sys.stderr。若同時列出了filename和stream兩個參數,則stream參數會被忽略。
# 
# format參數中可能用到的格式化串:
# %(name)s 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用戶輸出的消息
相關文章
相關標籤/搜索