Pregunta

I have been trying to get logging to work with a basic configuration via log4j2.xml, in a Spring project. Tried for two days with no luck. The project seems to ignore my log4j2.xml file and pick up the default configuration.

This is what I have been doing:

  1. Created log4j2.xml and added it to the class path. Configuration given below.
  2. Ensured that this xml file is part of the Deployment Assembly, hoping that it gets picked up
  3. Unzipped the war file generated to see if the file is being added as part of the deployment... and yes it was.

None helped. I looked up several postings to such problems on StackOverflow but most of it mentioned that all we have to do is add the file to classpath. But in my case, only the default configuration is what I see as being active, when I run the app. To be precise, though the below config says "info", the statement log.info("Inside foo()"); doesn't print the message. When I step in, I see that Info level logs are not enabled.

Any help will be greatly appreciated.

==================

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="INFO">
    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%d{ISO8601}] [%t] %-5p %c{6} - %msg%n"/>
        </Console>
        <File name="File" fileName="example.log">
            <PatternLayout pattern="[%d{ISO8601}] [%t] %-5p %c{6} - %msg%n"/>
        </File>
    </appenders>
    <loggers>
        <logger name="com.mypackage" level="info" additivity="false">
            <appender-ref ref="Console"/>
            <appender-ref ref="File"/>
        </logger>
        <root level="info">
            <appender-ref ref="Console"/>
        </root>
    </loggers>
</configuration>

Dependencies in my gradle build script:

def tomcatVersion = '7.0.42'
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
    exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
}

compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
compile group: 'com.datastax.cassandra', name: 'cassandra-driver-core', version:'1.0.3'
//compile group: 'com.yammer.metrics', name: 'metrics-core', version:'2.2.0'
compile 'org.springframework:spring-core:3.2.3.RELEASE'
compile 'org.springframework:spring-webmvc:3.2.3.RELEASE'
compile 'org.springframework.security:spring-security-web:3.2.0.RELEASE'
compile 'org.springframework.security:spring-security-core:3.2.0.RELEASE'
compile 'org.springframework.security:spring-security-config:3.2.0.RELEASE'
compile 'org.springframework.hateoas:spring-hateoas:0.7.0.RELEASE'

providedCompile 'javax.servlet:javax.servlet-api:3.0.1'

runtime 'com.fasterxml.jackson.core:jackson-core:2.2.2'
runtime 'com.fasterxml.jackson.core:jackson-databind:2.2.2'
runtime 'javax.xml.bind:jaxb-api:2.2.9'

compile 'org.apache.logging.log4j:log4j-jcl:2.0-rc1'
runtime 'org.apache.logging.log4j:log4j-jcl:2.0-rc1'

// compile 'org.slf4j:slf4j-api:1.7.5' // runtime 'org.slf4j:slf4j-jdk14:1.7.5'

testCompile group: 'junit', name: 'junit', version: '4.+'
testCompile 'org.springframework:spring-test:3.2.3.RELEASE'
testCompile "org.mockito:mockito-all:1.9.5"
¿Fue útil?

Solución

You need to have the log4j2 api jar and the log4j2 core jar in the classpath, as well as the log4j2.xml file. If either the core jar or the log4j2.xml cannot be found on the classpath, log4j2 will auto-configure to a default configuration that only prints ERROR-level messages to the console.

Try printing this from your application to see if the necessary jars are on the classpath:

//api
System.out.println(org.apache.logging.log4j.Logger.class.getResource("/org/ap‌​ache/logging/log4j/Logger.class"));
//core
System.out.println(org.apache.logging.log4j.Logger.class.getResource("/org/ap‌​ache/logging/log4j/core/Appender.class"));
//config
System.out.println(org.apache.logging.log4j.Logger.class.getResource("/log4j2.xml"));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top