??????? 不同的服務(wù)器對(duì)于使用log4j是有些不同的,實(shí)際使用中主要是用tomcat和jboss兩類,對(duì)于tomcat,它本身是沒有配置log4j的,所以使用起來和常規(guī)的一樣;而在jboss中它是本身配置了log4j的,所以有時(shí)候在看項(xiàng)目代碼時(shí),其整個(gè)項(xiàng)目并沒有l(wèi)og4j的配置文件,而在一些類中仍然定義了Logger,例如static Logger log = org.apache.log4j.Logger.getLogger(UserDaoImple.class);,這就表明開發(fā)者打算使用jboss默認(rèn)的log4j的配置,我們可以在jboss下的對(duì)應(yīng)的log目錄下的server.log中看到日志,jboss本身的log4j的配置是將debug,info級(jí)的日志寫在server.log中,而像error等級(jí)別比較高的日志打印到控制臺(tái)上,而寫到server.log中的日志比較多,并不方便查看。于是我們想到使用自己的log4j配置寫到某個(gè)具體的文件中(注意文件要先建立,才能忘里面寫東西,log4j自己不能建立文件),但這里因?yàn)閖boss有它自己的log4j配置,所以如果我們配置的log4j包含Console的Appender時(shí),就會(huì)出錯(cuò),錯(cuò)誤類似于
ERROR: invalid console appender config detected, console stream is looping.
解決方法一是不用Console的Appender,或者改jboss的配置文件,在jboss-service.xml文件里,把
<mbean code="org.jboss.logging.Log4jService" name="jboss.system:type=Log4jService,service=Logging">
????????<attribute name="ConfigurationURL">resource:log4j.xml</attribute>
????????<attribute name="CatchSystemOut">false</attribute>
????????<attribute name="Log4jQuietMode">true</attribute>
</mbean>。
我建議不用Console的Appender,當(dāng)然這是對(duì)jboss3.2.x是這樣,對(duì)于jboss4.0.x如果我們要用自己的log4j配置照上述改還是會(huì)有問題,會(huì)有類似于
log4j:ERROR A "org.jboss.logging.util.OnlyOnceErrorHandler" object is not assignable to a "org.apache.log4j.spi.ErrorHandler" variable
的異常,解決方法是把/server/default/jbossweb-tomcat55.sar/META-INF/jboss-service.xml 中的以下兩個(gè)熟悉改成true
<attribute name="Java2ClassLoadingCompliance">true</attribute>
<attribute name="UseJBossWebLoader">true</attribute>
以上就是使用jboss服務(wù)器可能出現(xiàn)的問題,解決了這些再來使用log4j就比較簡(jiǎn)單了。