Solved

Log4j - Implementation/Customization

Posted on 2010-09-07
11
685 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:MatrixStar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
11 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 33623042
0
 

Author Comment

by:MatrixStar
ID: 33623781
Though, I use AsyncAppender, I may not able to keep a separate buffer for my each request (thread)..right?
0
 
LVL 92

Expert Comment

by:objects
ID: 33624035
thats right, though the point of async appenders is that the logs are written from a seperate thread to the calling thread
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 33624039
you can include the thread name in the log message format if you need to track which thread logged what
0
 

Author Comment

by:MatrixStar
ID: 33654628
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
0
 

Author Comment

by:MatrixStar
ID: 33668744
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 -->
 
 
 
 


0
 

Author Comment

by:MatrixStar
ID: 33668772
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?
0
 
LVL 92

Expert Comment

by:objects
ID: 33668780
no it doesn't wait
0
 

Author Comment

by:MatrixStar
ID: 33781705
I'm going with AsyncAppender as of now, will keep post if I find any other best solution. Thanks
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 37158222
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question