2

I am trying to make all Log4J 2 loggers asynchronous with IMAP Disruptor. I have the disruptor dependencies correctly set, and in IntelliJ, I have set the following system property under VM options.

-DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector

My log4j2.xml file is this.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Appenders>
    <Console name="Console-Appender" target="SYSTEM_OUT">
        <PatternLayout>
            <pattern>
                [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
            </pattern>>
        </PatternLayout>
    </Console>
    <File name="File-Appender" fileName="logs/xmlfilelog.log" >
        <PatternLayout>
            <pattern>
                [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
            </pattern>
        </PatternLayout>
    </File>
</Appenders>
<Loggers>
    <Logger name="guru.springframework.blog.log4j2async" level="debug">
        <AppenderRef ref="File-Appender"/>
    </Logger>
    <Root level="debug">
        <AppenderRef ref="Console-Appender"/>
    </Root>
</Loggers>
</Configuration>

My logger class have this code.

package guru.springframework.blog.log4j2async;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log4J2AsyncLogger {

private static Logger logger = LogManager.getLogger();
public Log4J2AsyncLogger(){
    logger.info("Logger created by Thread Id:"+Thread.currentThread().getId());
}
public void performSomeTask(){
        logger.debug("This is a debug message sent by Thread Id:" + Thread.currentThread().getId());
        logger.info("This is a info message sent by Thread Id:" + Thread.currentThread().getId());
        logger.warn("This is a warn message sent by Thread Id:" + Thread.currentThread().getId());
        logger.error("This is a error message sent by Thread Id:" + Thread.currentThread().getId());
        logger.fatal("This is a fatal message sent by Thread Id:" + Thread.currentThread().getId());
 }
}

I expected the output of log messages with different thread IDs. But, both the console and file outputs are:

[INFO ] 2016-03-25 11:41:01.189 [main] Log4J2AsyncLogger - System property Log4jContextSelector: org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
[INFO ] 2016-03-25 11:41:01.189 [main] Log4J2AsyncLogger - Logger created by Thread Id:1
[DEBUG] 2016-03-25 11:41:01.189 [main] Log4J2AsyncLogger - This is a debug message sent by Thread Id:1
[INFO ] 2016-03-25 11:41:01.189 [main] Log4J2AsyncLogger - This is a info message sent by Thread Id:1
[WARN ] 2016-03-25 11:41:01.189 [main] Log4J2AsyncLogger - This is a warn message sent by Thread Id:1
[ERROR] 2016-03-25 11:41:01.189 [main] Log4J2AsyncLogger - This is a error message sent by Thread Id:1
[FATAL] 2016-03-25 11:41:01.189 [main] Log4J2AsyncLogger - This is a fatal message sent by Thread Id:1

In the logger class, I tried using a for loop with 1000 loops to log the messages, but still the same main thread is doing all the work. What am I doing wrong?

1
  • You can add -Dlog4j2.debug=true to your VM options and then you should see the async logger being used by Log4J2. Commented Jun 17, 2024 at 10:09

1 Answer 1

5

Log4j creates a snapshot of the message in the calling thread (your application thread). It will be written to disk in a separate background thread, but that will not affect the message content.

The thread name or ID of the background thread is never shown in the log. This is by design.

Sign up to request clarification or add additional context in comments.

3 Comments

As per my understanding in case of async logger in log file log statement's datetime will be same as when log statement was executed.
Takes a "snapshot" how? Deep-copying objects not designed for deep copy is always problematic in Java.
I would assume it just snapshots \ copies the String values that are required for the logging statement. In other words, it doesn't have to perform a "deep copy" of your object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.