You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by "LC (JIRA)" <ji...@apache.org> on 2015/09/30 06:10:04 UTC

[jira] [Created] (LOG4J2-1141) resources/log4j2.xml is not loaded if a subproject contains another config file

LC created LOG4J2-1141:
--------------------------

             Summary: resources/log4j2.xml is not loaded if a subproject contains another config file
                 Key: LOG4J2-1141
                 URL: https://issues.apache.org/jira/browse/LOG4J2-1141
             Project: Log4j 2
          Issue Type: Bug
          Components: Lookups
    Affects Versions: 2.4
         Environment: Tomcat 8.0.26, Gradle 2.7, Spring Boot 1.2.6, Log4j2 2.4 (and Eclipse Mars (4.5.0))
            Reporter: LC


{code:title=build.gradle}
buildscript {
    repositories.mavenCentral()
    dependencies.classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE'
}
subprojects {
    apply plugin: 'spring-boot'
    apply plugin: 'eclipse-wtp'
    sourceCompatibility = '1.8'
    repositories.mavenCentral()
    dependencies {
        compile 'org.apache.logging.log4j:log4j-api:2.4'
        compile 'org.apache.logging.log4j:log4j-core:2.4'
        compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.4'
        compile 'org.apache.logging.log4j:log4j-web:2.4'
    }
    task wrapper(type: Wrapper) {
        gradleVersion = '2.7'
    }
    eclipse.classpath {
        containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
        containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
    }
}
{code}

{code:title=settings.gradle}
include 'child', 'main'
{code}

{code:title=main/build.gradle}
apply plugin: 'war'
configurations.compile.exclude module : 'spring-boot-starter-logging'
dependencies {
    compile project(':child')
    compile 'org.springframework.boot:spring-boot-starter-log4j2'
    compile 'org.springframework.boot:spring-boot-starter-web'
}
eclipse.wtp.facet.file.withXml { n -> n.asNode().installed.find { it.@facet == 'jst.web' }.@version = '3.1' }
{code}

{code:title=main/src/main/java/loggertest/Application.java}
package loggertest;

import java.net.URI;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ServletContextInitializer;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.web.context.WebApplicationContext;

@SpringBootApplication
public class Application {

    public static class Initializer extends SpringBootServletInitializer implements ServletContextInitializer {
        @Override
        protected SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
            LogManager.getLogger(Application.class).debug("configure()");
            return application.sources(Application.class);
        }

        @Override
        protected WebApplicationContext run(final SpringApplication application) {
            application.addListeners(new LoggerContextInfo());
            return super.run(application);
        }
    }

    static class LoggerContextInfo implements ApplicationListener<ApplicationEvent> {
        @Override
        public void onApplicationEvent(final ApplicationEvent event) {
            final LoggerContext ctx = LoggerContext.getContext();
            final URI loc = ctx.getConfigLocation();
            System.err.println(String.format("\nevent = %s\nlogCtx.cfg@%X    = %s\nlogCtx.cfgLoc@%X = %s\n", event.getClass(),
                    ctx.getConfiguration().hashCode(), ctx.getConfiguration(), (loc != null ? loc.hashCode() : 0), loc));
        }
    }

    public static void main(final String[] args) {
        SpringApplication.run(new Class[] { Application.class, Initializer.class }, args);
    }
}
{code}

{code:xml|title=main/src/main/webapp/WEB-INF/web.xml - see issue: LOG4J2-873}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1" metadata-complete="false">
    <display-name>dummy</display-name>
</web-app>
{code}

{code:xml|title=main/src/main/resource/log4j2.xml}
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" strict="true">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="MY_LOGGER %-5p %d [%t][%c{1}]: %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="org.springframework" level="WARN"/>
        <Logger name="loggertest" level="DEBUG"/>
        <Root level="DEBUG">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>
{code}

{code:title=child/build.gradle}
startScripts.enabled = false
{code}

{code:xml|title=child/src/main/resources/log4j2-test.xml}
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" strict="true">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="CHILD_LOGGER %-5p %d [%t][%c{1}]: %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="org.springframework" level="WARN"/>
        <Logger name="loggertest" level="DEBUG"/>
        <Root level="DEBUG">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>
{code}


Results with *main/src/main/resources/log4j2.xml*:
{code}
event = class org.springframework.boot.context.event.ApplicationStartedEvent
logCtx.cfg@37E5AA81    = XmlConfiguration[location=jar:file:/[...]/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/main/WEB-INF/lib/child.jar!/log4j2-test.xml]
logCtx.cfgLoc@0 = null

event = class org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
logCtx.cfg@B9FC71B    = XmlConfiguration[location=jar:file:/[...]/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/main/WEB-INF/lib/child.jar!/log4j2-test.xml]
logCtx.cfgLoc@0 = null
{code}

Moving log4j2.xml from resources to *main/src/main/webapp/WEB-INF* will let log4j2 initialize with the correct config file, however thanks to another [issue in spring-boot|https://github.com/spring-projects/spring-boot/issues/4055] this will be overwritten (note how the configLocation field would still point to the original file):
{code}
event = class org.springframework.boot.context.event.ApplicationStartedEvent
logCtx.cfg@5F77D5DB    = XmlConfiguration[location=[...]\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\main\WEB-INF\log4j2.xml]
logCtx.cfgLoc@E93E026A = file:/[...]/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/main/WEB-INF/log4j2.xml

event = class org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
logCtx.cfg@F8561F3    = XmlConfiguration[location=jar:file:/[...]/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/main/WEB-INF/lib/spring-boot-1.2.6.RELEASE.jar!/org/springframework/boot/logging/log4j2/log4j2.xml]
logCtx.cfgLoc@E93E026A = file:/[...]/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/main/WEB-INF/log4j2.xml
{code}

Duplication the config file in *main/src/main/resources/log4j2.xml* and *main/src/main/webapp/WEB-INF* "solves" the issue -- if you consider code duplication a solution. (The new config object is the result of the aforementioned spring-boot bug.)
{code}
event = class org.springframework.boot.context.event.ApplicationStartedEvent
logCtx.cfg@35DFF1ED    = XmlConfiguration[location=[...]\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\main\WEB-INF\log4j2.xml]
logCtx.cfgLoc@E93E026A = file:/[...]/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/main/WEB-INF/log4j2.xml

event = class org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
logCtx.cfg@59A0B4D    = XmlConfiguration[location=[...]\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\main\WEB-INF\log4j2.xml]
logCtx.cfgLoc@E93E026A = file:/[...]/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/main/WEB-INF/log4j2.xml
{code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org