1

I am new to log4j. I'm converting an application from log4j 1.2 to log4j2. In the log4j.properties file I found follwoing configurations.

#############################################################
#      Default Logging Configuration File
############################################################

############################################################
#      Global properties
############################################################
handlers= java.util.logging.ConsoleHandler
.level= WARNING
############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
com.xyz.foo.level = SEVERE

How can I convert this configurations to log4j2 configuration ?

Thanks!

1 Answers1

4

Log4j2 has an (experimental) conversion tool in the log4j-1.2-api module.

The class is org.apache.log4j.config.Log4j1ConfigurationConverter. In addition to the log4j-1.2-api module you need JCommander (http://jcommander.org) on the classpath.


If you're looking for an example Log4j2 configuration with a Console and a File appender, try this:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <File name="MyFile" fileName="all.log" append="false">
            <!-- alternatively use XmlLayout (requires Jackson, see documentation for dependencies) -->
            <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Logger name="com.xyz.foo" level="warn" />
        <Root level="trace">
            <AppenderRef ref="Console" level="info" />
            <AppenderRef ref="MyFile"/>
        </Root>
    </Loggers>
</Configuration>

Save this to a file named log4j2.xml and put it in the classpath of your application.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • Getting error NoSuchMethodError:com.beust.jcommander.JCommander.setCaseSensitiveOptions(Z)V. Also I am not able to find equivalent syntax for above configuration in log4j2. – Trupti musmade Jan 25 '17 at 06:47
  • I could execute the class now but getting output as : OK = 0, Failures = 0, Total = 0 (java org.apache.log4j.config.Log4j1ConfigurationConverter --recurse /home/myProject --in /home/myProject/logging.properties --verbose -o aaaaa.txt). I dont see output file generated. Also from the codebase(https://fossies.org/linux/misc/apache-log4j-2.7-src.tar.gz/apache-log4j-2.7-src/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java) I feel this tool does not convert java.util.logging related configuration. – Trupti musmade Jan 25 '17 at 08:42
  • Can you please suggest me a way out to convert this configuration into log4j2 compatible configuration ? – Trupti musmade Jan 25 '17 at 08:53
  • Updated my answer. May not be an exact match but fairly close. The Log4j2 manual has many examples. – Remko Popma Jan 25 '17 at 10:59
  • JCommander is not needed (anymore) as a dep. anymore. From the latest JavaDoc: Run with "--help" on the command line. Example: java org.apache.log4j.config.Log4j1ConfigurationConverter --recurse E:\vcs\git\apache\logging\logging-log4j2\log4j-1.2-api\src\test\resources\config-1.2\hadoop --in log4j.properties --verbose – Gernot Feb 16 '22 at 16:54