Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i run my log asynchronously using log4j 1.x with log4j.properties file?

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.

like image 913
umar faraz Avatar asked May 30 '16 12:05

umar faraz


People also ask

Is log4j 1 x affected?

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.

Does log4j properties work with log4j2?

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 ".


2 Answers

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.)

like image 112
Remko Popma Avatar answered Sep 30 '22 13:09

Remko Popma


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

  1. Using log4j.xml Configure AsyncAppender as below
<?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>
  1. Programmatically You can configure AsyncAppender programmatically as below
    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

like image 24
Anil Agrawal Avatar answered Sep 30 '22 13:09

Anil Agrawal