我想在我的java web应用程序中使用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
我对我的应用程序中包含的lib所使用的日志库感到有些困惑,所以我必须包含commons.logging,slf4和apache log4j version 2.0-beta5。
我已经创建了log4j2.xml文件并将其放在classpath中,这是我的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?
谢谢。
答案 0 :(得分:2)
您需要将slf4j-log4j
网桥JAR文件替换为slf4j-log4j2
网桥JAR。您可以在此处阅读:http://logging.apache.org/log4j/2.x/log4j-slf4j-impl/
一旦你完成了,log4j依赖关系也应该消失......除非其他东西直接使用它。
要了解发生了什么,请参阅slf4j的文档。
FWIW,解析错误看起来像是由于log4j尝试(并且失败)解析log4j2格式的配置文件。 (这不会起作用,但我怀疑你是否打算这样做。)
答案 1 :(得分:0)
我不确定,但可能以下行表示log4j已添加到您的类路径中,并且必须导致log4j执行而不是log4j2:
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.7.5:compile
[INFO] | \- log4j:log4j:jar:1.2.17:compile
根据迁移指南的建议,我认为您应该首先从类路径中删除所有log4j依赖项。 尝试从上面的jar中排除log4j依赖项。
希望这有帮助。