web xml中關於log4j的設定及範例說明

2021-04-13 05:44:40 字數 1620 閱讀 4245

在web.xml有幾個條目和log4j有關,它們是: 

1. petclinic.root

2. log4jconfiglocation

/web-infclasseslog4j.properties

3.(該條目在petclinic中被注釋掉了)

class>org.springframework.web.util.log4jconfiglistenerclass>

a.用servlet或者servletcontextlistener對log4j做配置。通常,你不需要親自編寫servlet或者 listener,比如直接利用log4j的com.apache.jakarta.log4j.log4jinit類,spring的 org.springframework.web.util.log4jconfigservlet和 org.springframework.web.util.se

rvletcontextlistener,這種方式配置靈活,可以通過引數條目自行指定log4j.properties的位置。 b.

把log4j 的預設配置檔案(log4j.properties)放在classpath中,通常是/web-inf/classes目錄下.這種方式是log4j的 預設配置方式,無須其他配置。缺點就是無法靈活的通過配置檔案來指定log4j.properties的檔案位置。在我們的petclinic專案中,因 為listener條目被注釋,所以採用的也是這種預設方式。

現在我們考慮listener條目沒有被注釋的情況,這種情況和註冊log4jconfigservlet的目的是一樣的,只是必須在支援listener的servlet container中使用。

publicstaticvoidillegalstateexception

string

root = servletcontext.getrealpath("/");

if(root ==null

system

.setproperty(key, root); }

再 回到log4jwebconfigurer.initlogging(getservletcontext()),接下來的行為是從web.xml中獲取 log4jconfiglocation和log4jrefreshinterval.前者指出log4j配置檔案(有可能是xml格式)的位置和名字, 後者則指出重新都取配置檔案的時間間隔.然後呼叫log4jconfigurer.initlogging()方法對log4j做配置。從 log4jconfigurer.initlogging()方法我們可以看到,針對不同格式的配置檔案(properties或者xml), log4jconfigurer採用不同的lo4j configurator類做配置,例如domconfigurator或者 propertyconfigurator。

至此,關於petclinic中關於log4j的配置,我們已經基本上弄清楚了。可是system物件中的

petclinic.root屬性在什麼時候使用呢?在web-inf/classes下面的log4j.properties檔案中,有這麼一句:

web xml中關於log4j的設定及範例說明

在web.xml有幾個條目和log4j有關,它們是 1.petclinic.root 2.log4jconfiglocation web infclasseslog4j.properties 3.該條目在petclinic中被注釋掉了 class org.springframework.web.ut...

關於log4j知識

log4j的作用 log4j是乙個日誌輸出的外掛程式專門用來進行日誌管理的,根據我的理解就是用來執行我們用來檢測程式bug的system.out.println 語句的,不過更為簡潔,格式也更好判斷,利用相關的符號,可以精確到某一行的問題點。log4j有兩種使用的形式,一種是利用我們建立的log4j...

log4j日誌系統 Log4j

1.1 log4j的三大核心元件 1.2 loggers 記錄器 1.4 layouts 布局 org.apache.log4j.htmllayout 以html 形式布局 org.apache.log4j.patternlayout 可以靈活地指定布局模式 org.apache.log4j.lay...