2

i want to use log4j2 in my java web application.

This is my dependency tree printed by 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

I'm a little confused about log library used by libs included in my application so i had to include commons.logging, slf4 and apache log4j version 2.0-beta5.

I've created log4j2.xml file and putted it in classpath, this is my 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>

When application starts in tomcat7 follow messages appears in eclipse console

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.

I think that messages are refers to log4j configuration and not log4j2. Is it right?

How i can configure it to use only log4j2 ?

Thank you.

2 Answers 2

2

You need to replace the slf4j-log4j bridge JAR file with the slf4j-log4j2 bridge JAR. You can read about it here: http://logging.apache.org/log4j/2.x/log4j-slf4j-impl/

Once you've done that the log4j dependency should disappear too ... unless something else is using it directly.

To understand what is going on, refer to the documentation for slf4j.


FWIW, the parse errors look like they are due to log4j trying (and failing) to parse a log4j2 format configuration file. (That ain't going to work, though I doubt that you are intending that to happen.)

Sign up to request clarification or add additional context in comments.

Comments

0

I am not sure but probably the following line indicates that log4j is getting added to your classpath and must be causing log4j to execute instead of log4j2:

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

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

As suggested by the migration guide I think you should remove all log4j dependencies from the classpath first. Try excluding the log4j dependency from the above jar.

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.