Home Tutorials Log4j TTCCLayout in Log4j
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

TTCCLayout in Log4j

Advertisement
In this part of Log4j tutorial we are going to discuss about the TTCCLayout which is extending DateLayout

TTCCLayout in Log4j

     

In this part of Log4j tutorial we are going to discuss about the TTCCLayout which is extending DateLayout. It is another layout format for representing log events output.

It consists of following in its output log event: 

  • time
  • thread
  • category (Level)
  • nested diagnostic context information
  • name (output message)

For example in our output as "16 [main] INFO TTCCLayoutExample.class - First Log".

Note : One of the most important thing while using TTCCLayout is that "You should not use same TTCCLayout instance from the different appenders because it is not fully thread safe.

Here is the example code of TTCCLayoutExample file :

TTCCLayoutExample.java

import org.apache.log4j.*;

public class TTCCLayoutExample {
  static Logger logger = Logger.getLogger("TTCCLayoutExample.class");
  public static void main(String[] args) {
  try
  {
  FileAppender fileappender = 
  new 
FileAppender(new TTCCLayout(),"TTCCoutput.txt");
  logger.addAppender(fileappender);
  logger.info("First Log");  
  logger.info("Second Log");  
  logger.info("Third Log");  
  logger.info("Fourth Log");  
  logger.info("See your TTCCoutput.txt");
  logger.info("Exiting from the Main method  ");
  }catch(Exception e){
  e.printStackTrace();
  }
  }
}

Output:

After compiling and executing TTCCLayoutExample.java you will have following output in  your TTCCoutput.txt file.

16 [main] INFO TTCCLayoutExample.class - First Log
16 [main] INFO TTCCLayoutExample.class - Second Log
16 [main] INFO TTCCLayoutExample.class - Third Log
16 [main] INFO TTCCLayoutExample.class - Fourth Log
16 [main] INFO TTCCLayoutExample.class - See your TTCCoutput.txt
16 [main] INFO TTCCLayoutExample.class - Exiting from the Main method

Download Source Code

     

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 12, 2008

Ask Questions?    Discuss: TTCCLayout in Log4j  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Alex
May 28, 2012
Time stamp

There is no example how the time stamp can be used. Can the time stamp be used only in the beggining of the log message or it can be embedded in the middle of it?
DMCA.com