Migrating from Log4j 1.xUsing the Log4j 1.x bridgePerhaps the simplest way to convert to using Log4j 2 is to replace the log4j 1.x jar file with Log4j 2's log4j-1.2-api.jar. However, to use this successfully applications must meet the following requirements:
Converting to the Log4j 2 APIFor the most part, converting from the Log4j 1.x API to Log4j 2 should be fairly simple. Many of the log statements will require no modification. However, where necessary the following changes must be made.
Configuring Log4j 2Although the Log4j 2 configuration syntax is different than that of Log4j 1.x, most, if not all, of the same functionality is available. Note that system property interpolation via the ${foo} syntax has been extended to allow property lookups from many different sources. See the Lookups documentation for more details. For example, using a lookup for the system property named catalina.base, in Log4j 1.x, the syntax would be ${catalina.base}. In Log4j 2, the syntax would be ${sys:catalina.base}. Log4j 1.x has a XMLLayout which is different from the XmlLayout in Log4j 2, the log4j-1.2-api module contains a Log4j1XmlLayout which produce output in the format as in Log4j 1.x. The Log4j 1.x SimpleLayout can be emulated with PatternLayout "%level - %m%n". The Log4j 1.x TTCCLayout can be emulated with PatternLayout "%r [%t] %p %c %notEmpty{%ndc }- %m%n". Both PatternLayout and EnhancedPatternLayout in Log4j 1.x can be replaced with PatternLayout in Log4j 2. The log4j-1.2-api module contains two pattern conversions "%ndc" and "%properties" which can be used to emulate "%x" and "%X" in Log4j 1.x PatternLayout ("%x" and %X" in Log4j 2 have a slightly different format). Below are the example configurations for Log4j 1.x and their counterparts in Log4j 2. Sample 1 - Simple configuration using a Console AppenderLog4j 1.x XML configuration
Log4j 2 XML configuration
Sample 2 - Simple configuration using a File Appender, XMLLayout and SimpleLayoutLog4j 1.x XML configuration
Log4j 2 XML configuration
Sample 3 - SocketAppenderLog4j 1.x XML configuration. This example from Log4j 1.x is misleading. The SocketAppender does not actually use a Layout. Configuring one will have no effect.
Log4j 2 XML configuration
Sample 4 - AsyncAppender and TTCCLayoutLog4j 1.x XML configuration using the AsyncAppender.
Log4j 2 XML configuration.
Sample 5 - AsyncAppender with Console and FileLog4j 1.x XML configuration using the AsyncAppender.
Log4j 2 XML configuration. Note that the Async Appender should be configured after the appenders it references. This will allow it to shutdown properly.
|