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

Code Sample for Custom Config has Syntax Errors

    XMLWordPrintableJSON

Details

    • Hide
      The text surrounding the sample seems correct.
      Is it possible to use the ConfigurationBuilder in this context?
      Show
      The text surrounding the sample seems correct. Is it possible to use the ConfigurationBuilder in this context?

    Description

      https://logging.apache.org/log4j/2.x/manual/customconfig.html#Hybrid

      The sample has several syntax errors.
      Some of these errors are due to an older API and some are more trivial errors.
      The following fragment corrects the more critical of these errors.

      @Plugin(name = "MyXMLConfigurationFactory", category = "ConfigurationFactory")
      @Order(10)
      public class MyXMLConfigurationFactory extends ConfigurationFactory {
      
          /**
           * Valid file extensions for XML files.
           */
          public static final String[] SUFFIXES = new String[] {".xml", "*"};
      
          /**
           * Return the Configuration.
           * @param source The InputSource.
           * @return The Configuration.
           */
          @Override
          public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) {
              return new MyXMLConfiguration(loggerContext, source);
          }
      
          /**
           * Returns the file suffixes for XML files.
           * @return An array of File extensions.
           */
          public String[] getSupportedTypes() {
              return SUFFIXES;
          }
      
      }
      public class MyXMLConfiguration extends XmlConfiguration { 
        public MyXMLConfiguration(LoggerContext loggerContext, ConfigurationSource configSource) { 
          super(loggerContext, configSource); 
        }
        @Override protected void doConfigure() { 
          super.doConfigure(); 
          final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); 
          final Configuration config = ctx.getConfiguration(); 
          final Layout layout = PatternLayout.createDefaultLayout(config); 
          final Appender appender = FileAppender.createAppender("target/test.log", "false", "false", "File", "true", "false", "false", "4000", layout, null, "false", null, config); 
      
          appender.start(); addAppender(appender); 
          AppenderRef[] refs = new AppenderRef[] {AppenderRef.createAppenderRef("File", null, null)}; 
          LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.INFO, "org.apache.logging.log4j", "true", refs, null, config, null );
          loggerConfig.addAppender(appender, null, null);
          addLogger("org.apache.logging.log4j", loggerConfig);
        }
      }
      

      This still has some deprecated API calls.
      The code in the pull request replaces the deprecated calls with builders.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              phreed Fredrick Eisele
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated: