LaVOZs

The World’s Largest Online Community for Developers

'; log4j2 - I want to migrate log4j to log4j 2. Need help in recreating the log4j 2.xml - LavOzs.Com

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="logfile" class="org.apache.log4j.RollingFileAppender">
        <param name="threshold" value="TRACE" />
        <param name="File" value=x.log" />
        <param name="MaxFileSize" value="10000KB"/>
        <param name="MaxBackupIndex" value="19"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{MM/dd/yyyy HH:mm:ss:SSS} %-5p [%t] (%30.30c) %x - %m%n"/>
        </layout>
    </appender>

Can someone give me the log4j 2.xml of this. How this should be migrated to log4j2. how it looks the code once replaced by log4j .xml

You can use as shown below.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>

    <Console name="STDOUT" target="SYSTEM_OUT">
        <param name="Target" value="System.out"/>
        <PatternLayout pattern="%d{MM/dd/yyyy HH:mm:ss:SSS} %-5p [%t] (%30.30c) %x - %m%n""/>
    </Console>

    <RollingFile name="rollingFile"
                 fileName="x.log"
                 filePattern="x-%d{MM-dd-yy}-%i.log.gz">
        <PatternLayout pattern="%d{MM/dd/yyyy HH:mm:ss:SSS} %-5p [%t] (%30.30c) %x - %m%n"" />
        <Policies>
            <SizeBasedTriggeringPolicy size="10000KB" />
        </Policies>
        <DefaultRolloverStrategy max="19"/>
    </RollingFile>

</Appenders>
<Loggers>

    <Logger name="file" level="INFO">
        <AppenderRef ref="rollingFile" />
    </Logger>

    <Root level="info">
        <AppenderRef ref="STDOUT"/>
        <AppenderRef ref="rollingFile" />
    </Root>
</Loggers>
</Configuration>
Related
Log4J2 - assigning file appender filename at runtime
How to customize log4j2 RollingFileAppender?
How to configure StatisticsCsvLayout in log4j2
log4j2 threshold for consoleappender
Migrating from Log4j 1.x to Log4j2
How to create multiple log files in log4j2 using property file?
Access spring profile in log4j2 for spring boot application