Log4j2 improvement compare with Log4j :
AsyncLogger :
Implemented by LMAX Disruptor technology (a lock-free inter-thread communication library, instead of queues, resulting in higher throughput and lower latency) .
Better performance than AsyncAppender(Internally Using JDK ArrayBlockingQueue )
Garbage free or Low garbage when running in web application .
Parameterized Messages
Migration
Add bridge jar provided by log4j2
Slf4j log4j-slf4j-impl.jar route log request to log4j2
Log4j log4j-1.2-api.jar (missing in our nexus repository)
Common Logging log4j-jcl.jar
MDC to ThreadContext.
To track all the logs related to a specific request by a transaction ID .
ThreadContext.put(key,value)
layout: %X{key}
Change Log Level at Runtime :
Loggng servlet
get All Loggers : LogManager.getCurrentLoggers() ---> LoggerContext.getLoggers()
set LogLevel : log.setLevel ---> Configurator.setLevel(loggerName, logLevel);
node
Appender:
RollingFileAppender:
-Dnode=node1
<RollingFile name="cache_log" fileName="log/aspen-data-service-cache-${sys:node}.log"
filePattern="log/aspen-data-service-cache-${sys:node}.log.%i">
<PatternLayout
pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS}][%X{user}][%X{requestId}]@[%X{method}][%t] %-5p [%logger{36}:%L] - %msg%n" />
<Policies>
<SizeBasedTriggeringPolicy size="50MB" />
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingFile>
FileAppender, RollingFileAppender, RollingRandomAccessFileAppender : (When set bufferedIO = true , log will be written to buffer . Better performance . Downside : buffer data might be lost when system got shutdown )
Console:
AsyncAppender: Wrap other append and write log in seperate thread . (Lock free AsyncLogger is better)
JDBC,Kafka
Enable AsyncLogger :
Add disruptor-3.0.0.jar(missing in our nexus repository) or higher was required.
All Loggers Asynchronous :
Set system property -Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector.
All normal loggers like <Root> and <Logger> will be asynchronous .
Mixture mode:
No need to set system property "log4j2.contextSelector" to any value .
Use the <asyncRoot> or <asyncLogger> configuration elements to specify the loggers that need to be asynchronous.
Externalize Log Configuration :
-Dlog4j.configurationFile=file:///filePath
web