Solved

Log4j - Implementation/Customization

Posted on 2010-09-07
11
675 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 26

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now