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

Configuration file loading ignored on jar-with-dependencies with java 11

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Not A Problem
    • Affects Version/s: 2.12.1, 2.13.0
    • Fix Version/s: None
    • Component/s: Configurators
    • Labels:
      None
    • Environment:

      openjdk version 11.0.6; Apache Maven 3.6.3; Ubuntu 19.10

      Description

      Sample project to ease reproduction : https://github.com/skyghis/testLog4j2

       

      I try to load configuration from an external file on a fat jar.

      I tried to load configuration from multiples methods without success.

      I kept two methods on my sample:

      if (!Files.isReadable(log4jConfiguration)) {
          throw new IllegalStateException();
      }
      
      LOGGER.info("startup");
      LOGGER.debug("startup"); // Not logged = OK
      
      // First try to load configuration
      Configurator.initialize(null, log4jConfiguration.toString());
      LOGGER.debug("with init"); // Should be logged
      
      // Second try to load configuration
      // From log4j FAQ : https://logging.apache.org/log4j/2.x/faq.html#reconfig_from_code
      final LoggerContext context = (LoggerContext) LogManager.getContext(false);
      context.setConfigLocation(log4jConfiguration.toUri());
      
      LOGGER.debug("reconfig from code"); // Should be logged
      LOGGER.info("shutdown"); // Should be logged with [OUTER] prefix
      

       

      The code works when running from IDE, but when executed with java -jar the external configuration is not loaded.

      Result when running from IDE:

      Result when running from jar:

       

      I expect to have the configuration file loaded when running my jar file like when running from IDE.

       

      Edit: 

       If I compile and execute the same code with java 8, the code works as expected :

      So the issue seem specific to Java11.

       

        Attachments

        1. image-2020-02-06-21-20-15-004.png
          9 kB
          SkyghiS
        2. image-2020-02-06-21-21-14-488.png
          19 kB
          SkyghiS
        3. image-2020-02-20-20-59-42-991.png
          79 kB
          Ralph Goers
        4. java 8.png
          29 kB
          SkyghiS

          Activity

            People

            • Assignee:
              rgoers Ralph Goers
              Reporter:
              SkyghiS SkyghiS
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: