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

Add ConfigurationBuilder for programmatic configuration.

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.1
    • 2.4
    • None

    Description

      I found this link which said:

      You could use the static method #initialize(String contextName, ClassLoader loader, String configLocation) in org.apache.logging.log4j.core.config.Configurator. (You can pass null for the class loader.) Be aware that this class is not part of the public API so your code may break with any minor release.

      This documentation is unclear because it points to a member function which needs a filename configLocation where as the topic is ┬╗without a configuration file┬ź.

      It shoud rather point to the member function org.apache.logging.log4j.core.config.Configurator.initialize(ClassLoader loader, ConfigurationSource source).

      Example:

      import org.apache.logging.log4j.core.config.ConfigurationSource;
      import org.apache.logging.log4j.core.config.Configurator;
      
      final String hardCodedXmlConfig =
              "<?xml version='1.0' encoding='UTF-8'?>\n" +
              "<Configuration status='INFO'>\n" +
              "  <Appenders>\n" +
              "    <Console name='Console' target='SYSTEM_OUT'>\n" +
              "      <PatternLayout pattern='%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n'/>\n" +
              "    </Console>\n" +
              "  </Appenders>\n" +
              "  <Loggers>\n" +
              "    <Root level='debug'>\n" +
              "      <AppenderRef ref='Console'/>\n" +
              "    </Root>\n" +
              "  </Loggers>\n" +
              "</Configuration>\n";
      try {
          Configurator.initialize(null, new ConfigurationSource(new ByteArrayInputStream(hardCodedXmlConfig.getBytes())));
      } catch (IOException e) {
          e.printStackTrace();
      }
      

      Attachments

        1. LOG4J2-952-4.patch
          109 kB
          Ralph Goers
        2. LOG4J2-952-3.patch
          109 kB
          Ralph Goers
        3. LOG4J2-952-2.patch
          95 kB
          Ralph Goers
        4. LOG4J2-952.patch
          90 kB
          Ralph Goers

        Issue Links

          Activity

            People

              rgoers Ralph Goers
              FreeJoe Joe Merten
              Votes:
              1 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: