Class Log4j1ConfigurationParser

java.lang.Object
com.randomnoun.common.log4j2.Log4j1ConfigurationParser

public class Log4j1ConfigurationParser extends Object
You know how log4j 1 can be configured via XML or property files, and how log4j 2 can be configured via maliciously and arbitrarily different XML or property files ?

This class allows you to configure log4j2 using log4j1-style property configuration files, which allows you to slowly migrate your clusterfuck of logging frameworks from log4j1 to log4j2 without having to redo your entire logging configuration, which by now is Turing-complete and giving recipe suggestions.

It's based on the org.apache.log4j.config.Log4j1ConfigurationParser that's bundled in the log4j-1.2-api artifact ( specifically, org.apache.logging.log4j:log4j-1.2-api:2.14.0 ), but:

  • buildConfigurationBuilder() can take a Properties object (as well as an InputStream)
  • Unknown log4j1 appenders are constructed (and wrapped in some kind of log4j2 adapter) instead of ignored
  • Unknown log4j2 appenders are constructed instead of ignored

The original Log4j1ConfigurationParser also converts a handful of "well-known" appenders into their log4j2 equivalents, which still happens in this class.

To use, run the following:

Log4j1ConfigurationParser lcp = new Log4j1ConfigurationParser();
ConfigurationBuilder<?> builder = lcp.buildConfigurationBuilder(props);
Configuration config = builder.build();
Configurator.reconfigure(config); 
 
Original comment block: Experimental parser for Log4j 1.2 properties configuration files. This class is not thread-safe.

From the Log4j 1.2 Javadocs:

All option values admit variable substitution. The syntax of variable substitution is similar to that of Unix shells. The string between an opening "${" and closing "}" is interpreted as a key. The value of the substituted variable can be defined as a system property or in the configuration file itself. The value of the key is first searched in the system properties, and if not found there, it is then searched in the configuration file being parsed. The corresponding value replaces the ${variableName} sequence. For example, if java.home system property is set to /home/xyz, then every occurrence of the sequence ${java.home} will be interpreted as /home/xyz.

Changes from the Log4j1ConfigurationParser in org.apache.logging.log4j:log4j-1.2-api:2.14.0

  • buildConfigurationBuilder() can take a Properties object (as well as an InputStream)
  • Unknown log4j2 appenders are constructed instead of ignored
  • Constructor Details

  • Method Details

    • buildConfigurationBuilder

      public org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder<org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration> buildConfigurationBuilder(InputStream input) throws IOException
      Parses a Log4j 1.2 properties configuration file in ISO 8859-1 encoding into a ConfigurationBuilder.
      Parameters:
      input - InputStream to read from is assumed to be ISO 8859-1, and will not be closed.
      Returns:
      the populated ConfigurationBuilder, never null
      Throws:
      IOException - if unable to read the input
      org.apache.logging.log4j.core.config.ConfigurationException - if the input does not contain a valid configuration
    • buildConfigurationBuilder

      public org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder<org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration> buildConfigurationBuilder(Properties properties)
    • _buildConfigurationBuilder

      protected org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder<org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration> _buildConfigurationBuilder()