2

我想在我的 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

我对我的应用程序中包含的库使用的日志库有点困惑,所以我必须包含 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 ?

谢谢你。

4

2 回答 2

2

您需要用网桥 JAR 替换slf4j-log4j网桥 JAR 文件slf4j-log4j2。你可以在这里阅读:http: //logging.apache.org/log4j/2.x/log4j-slf4j-impl/

一旦你完成了 log4j 依赖也应该消失......除非有其他东西直接使用它。

要了解发生了什么,请参阅 slf4j 的文档。


FWIW,解析错误看起来像是由于 log4j 尝试(并且失败)解析 log4j2 格式配置文件。(那是行不通的,尽管我怀疑你是否打算这样做。)

于 2013-05-03T13:35:09.807 回答
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 依赖项。

希望这可以帮助。

于 2016-02-10T06:41:14.627 回答