python logging模組配置日誌檔案

2021-08-22 02:25:51 字數 1668 閱讀 9462

通過logging.config模組配置日誌

#logger.conf

[loggers] ;設定日誌記錄器 :根目錄,簡單例項量種型別的日誌

keys=root,******example

[handlers] ;設定日誌處理器:控制台處理,檔案處理

keys=consolehandler,filehandler

[formatters] ;設定格式化器:

keys=******formatter

[logger_root] ;根部日誌記錄器,從debug開始,日誌處理器為檔案處理

level=debug

handlers=filehandler

[logger_******example] ;簡單例項日誌記錄器,從debug開始,日誌處理器為控制台處理及檔案處理

level=debug

handlers=consolehandler,filehandler

qualname=******example

propagate=0

[handler_consolehandler] ;控制台處理:字串輸出,從warning開始,標準化輸出

class=streamhandler

level=warning

formatter=******formatter

args=(sys.stdout,)

[handler_filehandler] ;控制台處理:檔案輸出,日誌回滾方式,從debug開始,以追加方式輸出到test,最多備份10個日誌檔案,每個日誌檔案最大20m

class=handlers.rotatingfilehandler

level=debug

formatter=******formatter

args=('test.log','a+',20*1024*1024,10) ;最多備份10個日誌檔案,每個日誌檔案最大20m

#[formatter_******formatter]

format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

datefmt=

#配置檔案時

import logging

import logging.config

logging.config.fileconfig('logging.ini')

#通過logging.basicconfig函式對日誌的輸出格式及方式做相關配置

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',

filemode='w')

logging.debug('this is debug message')

logging.info('this is info message')

logging.warning('this is warning message')

Python logging模組學習

import logging 日誌級別列表,預設為logging.warning levels logging.notset,logging.debug,logging.info,logging.warning,logging.error,logging.critical log format as...

python logging模組簡介

logging模組是python內建的標準模組,主要用於輸出執行日誌,可以設定輸出日誌的等級 日誌儲存路徑 日誌檔案回滾等。相對於print,該模組具有可以決定在列印什麼級別的資訊和將資訊輸出放置在什麼地方的優點。配置logging的基本設定,並在控制台輸出 import logging loggi...

Python logging日誌模組

1.日誌的級別 日誌一共分成5個等級,從低到高分別是 1 debug 2.info 3.warning 4.error 5.critical說明 這5個等級,也分別對應5種打日誌的方法 debug info warning error critical。預設的是 warning,當在warning或...