?
Solved

Log4j - Implementation/Customization

Posted on 2010-09-07
11
Medium Priority
?
693 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
10 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

864 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