2013-05-03 89 views
2

我想在我的Java Web應用程序中使用log4j2。Java如何配置log4j2

這是我的依賴關係樹印Maven的

[INFO] com.mypackage.app:APP:war:1.0-SNAPSHOT 
[INFO] +- org.codehaus.jackson:jackson-core-asl:jar:1.9.12:compile 
[INFO] +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.12:compile 
[INFO] +- javax.servlet:servlet-api:jar:2.5:provided 
[INFO] +- junit:junit:jar:4.11:test 
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test 
[INFO] +- org.springframework:spring-test:jar:3.2.2.RELEASE:test 
[INFO] +- javax.mail:mail:jar:1.4.6:compile 
[INFO] | \- javax.activation:activation:jar:1.1:compile 
[INFO] +- org.springframework:spring-core:jar:3.2.2.RELEASE:compile 
[INFO] +- org.springframework:spring-context:jar:3.2.2.RELEASE:compile 
[INFO] | +- org.springframework:spring-aop:jar:3.2.2.RELEASE:compile 
[INFO] | \- org.springframework:spring-expression:jar:3.2.2.RELEASE:compile 
[INFO] +- org.springframework:spring-context-support:jar:3.2.2.RELEASE:compile 
[INFO] +- org.springframework:spring-beans:jar:3.2.2.RELEASE:compile 
[INFO] +- org.springframework:spring-web:jar:3.2.2.RELEASE:compile 
[INFO] | \- aopalliance:aopalliance:jar:1.0:compile 
[INFO] +- org.springframework:spring-webmvc:jar:3.2.2.RELEASE:compile 
[INFO] +- org.springframework:spring-tx:jar:3.2.2.RELEASE:compile 
[INFO] +- org.springframework:spring-orm:jar:3.2.2.RELEASE:compile 
[INFO] | \- org.springframework:spring-jdbc:jar:3.2.2.RELEASE:compile 
[INFO] +- org.springframework.security:spring-security-core:jar:3.1.3.RELEASE:compile 
[INFO] +- org.springframework.security:spring-security-web:jar:3.1.3.RELEASE:compile 
[INFO] +- org.springframework.security:spring-security-config:jar:3.1.3.RELEASE:compile 
[INFO] +- org.apache.tiles:tiles-jsp:jar:3.0.1:compile 
[INFO] | +- org.apache.tiles:tiles-servlet:jar:3.0.1:compile 
[INFO] | | +- org.apache.tiles:tiles-core:jar:3.0.1:compile 
[INFO] | | \- org.apache.tiles:tiles-request-servlet:jar:1.0.1:compile 
[INFO] | +- org.apache.tiles:tiles-template:jar:3.0.1:compile 
[INFO] | | +- org.apache.tiles:tiles-api:jar:3.0.1:compile 
[INFO] | | \- org.apache.tiles:tiles-autotag-core-runtime:jar:1.1.0:compile 
[INFO] | \- org.apache.tiles:tiles-request-jsp:jar:1.0.1:compile 
[INFO] |  \- org.apache.tiles:tiles-request-api:jar:1.0.1:compile 
[INFO] +- javax.servlet:jstl:jar:1.2:compile 
[INFO] +- asm:asm:jar:3.1:compile 
[INFO] +- cglib:cglib-nodep:jar:2.1_3:compile 
[INFO] +- org.hibernate:hibernate-core:jar:4.1.9.Final:compile 
[INFO] | +- antlr:antlr:jar:2.7.7:compile 
[INFO] | +- org.jboss.logging:jboss-logging:jar:3.1.0.GA:compile 
[INFO] | +- org.javassist:javassist:jar:3.17.1-GA:compile 
[INFO] | +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec:jar:1.0.0.Final:compile 
[INFO] | +- dom4j:dom4j:jar:1.6.1:compile 
[INFO] | +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile 
[INFO] | \- org.hibernate.common:hibernate-commons-annotations:jar:4.0.1.Final:compile 
[INFO] +- commons-dbcp:commons-dbcp:jar:20030825.184428:compile 
[INFO] +- commons-pool:commons-pool:jar:20030825.183949:compile 
[INFO] +- commons-collections:commons-collections:jar:20040616:compile 
[INFO] +- commons-lang:commons-lang:jar:20030203.000129:compile 
[INFO] +- commons-beanutils:commons-beanutils:jar:20030211.134440:compile 
[INFO] +- commons-digester:commons-digester:jar:2.1:compile 
[INFO] +- mysql:mysql-connector-java:jar:5.1.22:compile 
[INFO] +- org.apache.logging.log4j:log4j-api:jar:2.0-beta5:compile 
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.0-beta5:compile 
[INFO] | \- com.sun:jconsole:jar:1.6.0:system 
[INFO] +- commons-logging:commons-logging:jar:1.1.2:compile 
[INFO] +- org.slf4j:slf4j-api:jar:1.7.5:compile 
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.7.5:compile 
[INFO] | \- log4j:log4j:jar:1.2.17:compile 
[INFO] +- joda-time:joda-time:jar:2.1:compile 
[INFO] +- joda-time:joda-time-hibernate:jar:1.3:compile 
[INFO] +- javax.validation:validation-api:jar:1.1.0.Beta2:compile 
[INFO] +- org.hibernate:hibernate-validator:jar:4.3.1.Final:compile 
[INFO] +- org.quartz-scheduler:quartz:jar:2.1.7:compile 
[INFO] | \- c3p0:c3p0:jar:0.9.1.1:compile 
[INFO] +- com.google.guava:guava:jar:14.0.1:compile 
[INFO] \- org.apache.httpcomponents:httpclient:jar:4.2.4:compile 
[INFO] +- org.apache.httpcomponents:httpcore:jar:4.2.4:compile 
[INFO] \- commons-codec:commons-codec:jar:1.6:compile 

我有點困惑日誌由庫使用的庫包含在我的應用程序,所以我不得不包括commons.logging,slf4和Apache log4j的2.0版-beta5。

我創建log4j2.xml文件,並在類路徑推杆吧,這是我的log4j2.xml

<?xml version="1.0" encoding="UTF-8"?> 
<configuration status="debug" packages="com.mypackage.app"> 
    <appenders> 
    <File name="APPENDER_FILE" fileName="logs/app.log" append="false"> 
     <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %p [%C:%L] - %m%n"/> 
    </File> 
    <Console name="APPENDER_OUT" target="SYSTEM_OUT"> 
     <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %p [%C:%L] - %m%n"/> 
    </Console> 
    </appenders> 
    <loggers> 
    <root level="debug"> 
     <appender-ref ref="APPENDER_OUT"/> 
     <appender-ref ref="APPENDER_FILE"/> 
    </root> 
    </loggers> 
</configuration> 

當應用程序開始在tomcat7遵循的消息出現在Eclipse控制檯

mag 03, 2013 2:46:37 PM org.apache.catalina.core.ApplicationContext log 
INFO: Initializing log4j from [classpath:log4j2.xml] 
log4j:WARN Continuable parsing error 2 and column 65 
log4j:WARN L'elemento radice "configuration" del documento deve corrispondere alla radice DOCTYPE "null". 
log4j:WARN Continuable parsing error 2 and column 65 
log4j:WARN Documento non valido: nessuna grammatica trovata. 
log4j:WARN The <configuration> element has been deprecated. 
log4j:WARN Use the <log4j:configuration> element instead. 
log4j:WARN Unrecognized element appenders 
log4j:WARN Unrecognized element loggers 
mag 03, 2013 2:46:37 PM org.apache.catalina.core.ApplicationContext log 
INFO: Initializing Spring root WebApplicationContext 
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader). 
log4j:WARN Please initialize the log4j system properly. 
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 

我認爲消息是指log4j配置而不是log4j2。這樣對嗎?

我如何配置它只使用log4j2?

謝謝。

回答

2

您需要用slf4j-log4j2網橋JAR替換slf4j-log4j網橋JAR文件。你可以在這裏閱讀:http://logging.apache.org/log4j/2.x/log4j-slf4j-impl/

一旦你這樣做了,log4j依賴應該消失了...除非別的東西直接使用它。

要了解正在發生的事情,請參閱slf4j的文檔。


FWIW,解析錯誤,看起來他們是由於嘗試的log4j(和失敗)來解析log4j2格式的配置文件。 (這不起作用,但我懷疑你是否打算這樣做)

0

我不確定,但可能下面的行表示log4j被添加到你的類路徑中,並且必須導致log4j執行而不是log4j2的:

[INFO] +- org.slf4j:slf4j-log4j12:jar:1.7.5:compile 

[INFO] | \- log4j:log4j:jar:1.2.17:compile 

正如所建議的遷移指南,我認爲你應該從classpath首先刪除所有的log4j的依賴。 嘗試從上面的jar中排除log4j依賴項。

希望這會有所幫助。