Issue
I am using logback with springboot. But now i need to migrate from logback to log4j2. I need to convert the follwing logback.xml into log4j2.properties. And what dependency should i need to add for log4j2. Please help me with this.
<property resource="application-logging.yml" />
<appender name = "STDOUT" class = "ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${fileName}/logfile.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${fileName}/logfile.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
</appender>
<logger name="com.samplelogging.logging.filter" level="INFO">
<appender-ref ref="INFO_FILE" />
</logger>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
Thanks in advance!
Solution
For Log4j2, you can use this dependency :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Also in the properties section of your pom.xml, you must specify the latest log4j2 version in which the vulnerability is fixed, i.e.
<properties>
<log4j2.version>2.17.0</log4j2.version>
</properties>
And this is my log4j2.xml :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %c:%L - %m%n
</Property>
<Property name="LOG_DIR">@log.dir@/@app.name@</Property>
<Property name="APP_NAME">@app.name@</Property>
</Properties>
<Appenders>
<Console name="Console">
<PatternLayout pattern="${LOG_PATTERN}" />
</Console>
<RollingFile name="rollingFileLogger"
fileName="${LOG_DIR}/${APP_NAME}.log"
filePattern="${LOG_DIR}/${APP_NAME}-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="${LOG_PATTERN}" />
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.myorg" level="info">
<AppenderRef ref="rollingFileLogger" />
</Logger>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
The @log.dir@
and @app.name@
are the placeholders for actual values from my application.properties
file.
To use log4j2.properties instead of log4j2.xml, sample :
status = warn
name= com.myorg
# Log files location
property.basePath = @log.dir@/@app.name@
# RollingFileAppender name, pattern, path and rollover policy
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName= "${basePath}/${APP_NAME}.log"
appender.rolling.filePattern= ${basePath}/app_%d{yyyyMMdd}.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
# Console appender configuration
appender.console.type = Console
appender.console.name = consoleLogger
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Configure root logger
rootLogger.level = info
rootLogger.appenderRef.rolling.ref = fileLogger
Answered By - Urvashi Soni