?
Solved

Log4j - Implementation/Customization

Posted on 2010-09-07
11
Medium Priority
?
687 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 …
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:
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

764 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