I am currently working on performance of "log4j 1.x" vs "logback" i.e. (slf4j).
I can append async to my logback, but i am not able to find any link which can async my log4j.
Async is only introduced in log4j 2.x? or is there any way to make my log4j 1.x work async.
Please assist me.
Thank you.
JDBCAppender in Log4j 1. x is vulnerable to SQL injection in untrusted data. This flaw allows a remote attacker to run SQL statements in the database if the deployed application is configured to use JDBCAppender with certain interpolation tokens. A flaw was found in the Java logging library Apache Log4j in version 1.
Log4j 2 doesn't support the Log4j v1 ". properties" format anymore (yet, since v2. 4, Log4j supports a Property format, but its syntax is totally different from v1 format). New formats are XML, JSON, and YAML, see the documentation (note: if you used one of these formats in a file called ".
Asynchronous logging is one of Log4j 2's strong points. Instead of the AsyncAppender, use Async Loggers: the performance is much better.
The Log4j 2 performance page compares Log4j-1.x, Logback and Log4j 2. Take a look and then decide what logging framework to use.
By the way, Log4j 2 has an adapter called log4j-1.2-api-2.6.jar
which lets your application use the old log4j-1.2 API, but uses the new log4j 2 implementation.
(Log4j-1.x also has an AsyncAppender. Just like the Log4j 2 AsyncAppender and the Logback AsyncAppender, it uses a BlockingQueue. Log4j 2's Async Loggers use a non-blocking data structure which is much faster.)
log4j.properties does not handle the advanced configuration features supported by the DOMConfigurator such as support custom ErrorHandlers, nested appenders such as the AsyncAppender, etc.
Refer log4j API Documentation
There are two ways to configure AsyncAppender in log4j 1.x
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console1" class="org.apache.log4j.ConsoleAppender">
<param name="target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n" />
</layout>
</appender>
<!--wrap ASYNC around other appender if you want -->
<appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="500" />
<!-- LocationInfo is Optional, use only if location info is required. (Default value : false)-->
<!-- <param name="LocationInfo" value="true" /> -->
<appender-ref ref="console1" />
<root>
<priority value="debug" />
<appender-ref ref="ASYNC" />
</root>
</log4j:configuration>
static {
enableAsyncAuditLog(Logger.getRootLogger());
}
private static void enableAsyncAuditLog(Logger targetLogger) {
Enumeration<Appender> appenders = targetLogger.getAllAppenders();
AsyncAppender asyncAppender = new AsyncAppender();
asyncAppender.setBufferSize(500);
asyncAppender.setLocationInfo(true); // Otherwise Class and Line info will not be available to logger
while (appenders.hasMoreElements()) {
Appender appender = appenders.nextElement();
if (!(appender instanceof AsyncAppender)) {
targetLogger.removeAppender(appender);
asyncAppender.addAppender(appender);
}
}
appenders = asyncAppender.getAllAppenders();
if (appenders != null && appenders.hasMoreElements()) {
targetLogger.addAppender(asyncAppender);
}
}
You can refer this project for reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With