Java如何配置log4j2

Java如何配置log4j2,java,logging,log4j2,Java,Logging,Log4j2,我想在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:se

我想在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
我对我的应用程序中包含的libs使用的日志库有点困惑,所以我必须包含commons.logging、slf4和apachelog4j版本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.
mag 032013 2:46:37 PM org.apache.catalina.core.ApplicationContext日志
信息:从[classpath:log4j2.xml]初始化log4j
log4j:WARN Continuable解析错误2和第65列
log4j:WARN L'elemento radice“configuration”del documento deve corrispondere alla radice DOCTYPE“null”。
log4j:WARN Continuable解析错误2和第65列
log4j:警告文件无效:语法错误。
log4j:警告元素已被弃用。
log4j:WARN改为使用元素。
log4j:警告无法识别的元素附加器
log4j:警告无法识别的元素记录器
2013年3月3日下午2:46:37 org.apache.catalina.core.ApplicationContext日志
信息:正在初始化Spring根WebApplicationContext
log4j:WARN找不到记录器(org.springframework.web.context.ContextLoader)的追加器。
log4j:警告请正确初始化log4j系统。
log4j:请参阅http://logging.apache.org/log4j/1.2/faq.html#noconfig 更多信息。
我认为消息是指log4j配置,而不是log4j2。是这样吗

如何将其配置为仅使用log4j2


谢谢。

您需要将
slf4j-log4j
网桥JAR文件替换为
slf4j-log4j2
网桥JAR文件。您可以在此处阅读:

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

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



FWIW,解析错误看起来像是由于log4j试图(但未能)解析log4j2格式的配置文件造成的。(这是行不通的,尽管我怀疑您是否打算这样做。)

我不确定,但可能下面的一行表示log4j正在添加到类路径中,并且必须导致log4j而不是log4j2执行:

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

[INFO] |  \- log4j:log4j:jar:1.2.17:compile
正如迁移指南所建议的,我认为应该首先从类路径中删除所有log4j依赖项。 尝试从上述jar中排除log4j依赖项

希望这有帮助