java工程整合log4J

來源:互聯網
上載者:User

標籤:log4j

log4j是Java開發平台中的日誌管理工具,是Apache基金會的一個開源項目,它的作用可以簡單的理解為在開發過程中替代system.out的功能。


1、在工程中整合log4j:

(1)在官網http://logging.apache.org/log4j/2.x/download.html中下載jar包並添加到build path中

(2)修改設定檔log4j.properties,可以百度一下,這種資源很多,我的設定檔如下:

log4j.rootLogger =DEBUG,console,file,D,E #設定記錄層級和日誌輸出目的地,層級只能有一個,目的地可以由多個log4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.appender.console.layout=org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n#warn:before write the file log.log, clear itlog4j.appender.file=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.file.file=./log.loglog4j.appender.file.Append = false #注意:每次寫檔案前,清空,做實驗用,預設值為truelog4j.appender.file.DatePattern='.'yyyy-MM-ddlog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n#log4j.appender.file.layout=org.apache.log4j.TTCCLayout#org.apache.log4j.TTCCLayout do not have the property such as ConversionPattern#test output different log information to seperate file#set the DEBUG level information output to the file debug.log#warn:before write the file debug.log, clear itlog4j.appender.D=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.D.file=./debug.loglog4j.appender.D.Append = false <span style="font-family: Arial, Helvetica, sans-serif;">#注意:每次寫檔案前,清空,做實驗用,預設值為true</span>log4j.appender.D.Threshold = DEBUG #只有比DEBUG層級高的日誌才會輸出到debug.log檔案中,相當於五種類型的日誌都會儲存到debug.log中log4j.appender.D.DatePattern='.'yyyy-MM-ddlog4j.appender.D.layout=org.apache.log4j.PatternLayoutlog4j.appender.D.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n#set the ERROR level information output to the file debug.log#warn:before write the file error.log, clear itlog4j.appender.E=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.E.file=./error.loglog4j.appender.E.Append = false <span style="font-family: Arial, Helvetica, sans-serif;">#注意:每次寫檔案前,清空,做實驗用,預設值為true</span>log4j.appender.E.Threshold = ERROR <span style="font-family: Arial, Helvetica, sans-serif;">#只有比ERROR層級高的日誌才會輸出到error.log檔案中,相當於error.log檔案只儲存FATAL和ERROR類型的日誌</span>log4j.appender.E.DatePattern='.'yyyy-MM-ddlog4j.appender.E.layout=org.apache.log4j.PatternLayoutlog4j.appender.E.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
(3)優先順序說明:log4j中有5種優先順序,需要注意的是優先順序資料越小其優先順序越高(0最高,7最低),當設定某種優先順序後,比其高的層級日誌都會輸出,例如,如果在log4j.rootLogger =DEBUG,console,file,D,E設定的為DEBUG,則所有層級的日誌都會輸出,因為DEBUG層級最低。這點在實現不同層級的日誌資訊寫到不同的檔案時非常重要。

FATAL 0
ERROR 3
WARN 4
INFO 6
DEBUG 7

2、log4j的初始化

在網上搜了下,發現有三種方式進行初始化:

(1)自動載入設定檔,這個對設定檔的存放位置有要求,我這裡的是Java se工程,設定檔放在src目錄下;若是放在工程根目錄下,載入失敗,會出現如下錯誤:

log4j:WARN No appenders could be found for logger (Log4jTest).log4j:WARN Please initialize the log4j system properly.
(2)手動載入設定檔,在main方法裡利用語句(注意根據設定檔的路徑自己調整):

PropertyConfigurator.configure("./log4j.properties");
(3)不用設定檔,直接在代碼中設定。

關於log4j的初始化過程,有篇部落格以xml格式的設定檔為例,從源碼層級上做瞭解釋http://tiangu.iteye.com/blog/1153777


3、log4j實現不同類型的日誌輸出到不同的檔案

這裡必須設定log4j.appender.xxx.Threshold屬性,只有比該層級高的日誌才會輸出到該記錄檔中。

詳見上面的設定檔注釋。















java工程整合log4J

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.