Trupti musmade
Trupti musmade

Reputation: 11

Convert log4j 1.2 configuration to log4j 2 configuration

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!

Upvotes: 1

Views: 6024

Answers (1)

Remko Popma
Remko Popma

Reputation: 36754

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.

Upvotes: 4

Related Questions