Link to home
Start Free TrialLog in
Avatar of MatrixStar
MatrixStar

asked on

Log4j - Implementation/Customization

I'm trying to implement Asynchronous logging in my app. My requirement is as mentioned below.


For Thread 1, Request Flow starts on Class A ---> Class B --> Class C ---> Class D ---> Classs E ........ -> Class M --> Flow ends

For Thread 2, Request Flow starts on Class A ---> Class B --> Class C ---> Class D ---> Classs E ........ -> Class M --> Flow ends

For Thread n, Request Flow starts on Class A ---> Class B --> Class C ---> Class D ---> Classs E ........ -> Class M --> Flow ends


This is my requirement: The log message needs to be persisted into DB as well as flat file through Asynchronous processing. I do not want to append the log message into my destination (destination would be JMSAppender ->DB) as soon as I invoke Logger methods (info, debug, warn..). I want to keep a separate buffer for each request thread and flush it into appender/destination at each mile stone (let's say for example, I would like to flush out the buffer in Class C and Class F, Class Class I ..)

I would like to know, what changes/design do we need to make in log4j to fit into my requirement Or Can I go without log4j?. Any suggestion please?

We want to make sure, this logging implementation should not lead to performance hit
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MatrixStar
MatrixStar

ASKER

Though, I use AsyncAppender, I may not able to keep a separate buffer for my each request (thread)..right?
thats right, though the point of async appenders is that the logs are written from a seperate thread to the calling thread
you can include the thread name in the log message format if you need to track which thread logged what
you are right, that's one way of doing that. I'm trying to find out a way collectively store message of one thread in a buffer and flush it out.

Anyway, Thanks
Though, I use AsyncAppender and buffer size 500, I see, the message is sent to target appender as soon as message is sent to AysyncAppender. any idea why is that?

Please look at my log4j configuration below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >

<log4j:configuration threshold="all" debug="true"  xmlns:log4j="http://jakarta.apache.org/log4j/">

  <appender name="jmsQueueAppender" class="com.log.JMSQueueAppender">
       <layout class="org.apache.log4j.PatternLayout">
           <param name="ConversionPattern" value="%-5p %t [%-40.40c] %x - %m%n"/>
      </layout>
  </appender>
 
  <appender name="asyncAppender" class="org.apache.log4j.AsyncAppender">
            <param name="BufferSize" value="500"/>
            <appender-ref ref="jmsQueueAppender"/>
  </appender>

 
 <!-- default Asynchronous Appender logging -->
 <logger name="async" additivity="false">
  <level value="DEBUG"/>
  <appender-ref ref="asyncAppender"/>

 
</log4j:configuration>




JMSQueueAppender


  package com.log;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

public class JMSQueueAppender extends AppenderSkeleton {

    public JMSQueueAppender() {
       
        System.out.println("JMSQueueAppender is instantiated...");
    }
   
    @Override
    protected void append(LoggingEvent event) {
        System.out.println("JMSQueueAppender Thread: " + Thread.currentThread().getName());
        System.out.println("JMSQueueAppender Appender test:" + event.getMessage());

    }

    @Override
    public void close() {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean requiresLayout() {
        // TODO Auto-generated method stub
        return false;
    }

}


        
Servlet
               
        public class SampleServlet extends HttpServlet {
   

    private static final Logger async = Logger.getLogger("async."+CommandServlet.class);
   
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       
        //console.info("console:Console log test.");

        System.out.println("CommandServlet Thread: " + Thread.currentThread().getName());
        async.info("Async:ASYNC log test.");
    }

}


 
 
 
           
           
 

 
 <!-- default Asynchronous Appender logging -->
 
 
 
 


Sorry for my earlier message got messed up. I couldn't re-edit it to corrrect,

My Question is, Doesn't Dispatcher thread wait until the buffer is getting filled up before send the message to destination?
no it doesn't wait
I'm going with AsyncAppender as of now, will keep post if I find any other best solution. Thanks
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.