Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-1141

resources/log4j2.xml is not loaded if a subproject contains another config file

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 2.4
    • Fix Version/s: None
    • Component/s: Lookups
    • Labels:
      None
    • Environment:

      Tomcat 8.0.26, Gradle 2.7, Spring Boot 1.2.6, Log4j2 2.4 (and Eclipse Mars (4.5.0))

      Description

      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'
          }
      }
      
      settings.gradle
      include 'child', 'main'
      
      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' }
      
      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);
          }
      }
      
      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>
      
      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>
      
      child/build.gradle
      startScripts.enabled = false
      
      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>
      

      Results with main/src/main/resources/log4j2.xml:

      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
      

      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 this will be overwritten (note how the configLocation field would still point to the original file):

      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
      

      Duplicating 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.)

      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
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              lesliel LC
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: