Home Tutorials Log4j Use of SimpleLayout in Log4j



Use of SimpleLayout in Log4j
Posted on: September 12, 2008 at 12:00 AM
In the previous example we have studied that there are many Layout classes for displaying logging output in the desired format.

Use of SimpleLayout in Log4j

     

In the previous example we have studied that there are many Layout classes for displaying logging output in the desired format. In this section we are going to describe you one of them, SimpleLayout, the simplest of all.

Example of SimpleLayout log message is as follows:
"Debug
- and debug log message".

In our example of SimpleLayoutExample we are using FileAppender to append the output log message to a log file "SimpleLog.log". First we have created an object of Logger class by calling static method getLogger(). After creating FileAppender object we have added this appneder to our logger with the addAppender() method. 

Here is the example code of SimpleLayoutExample class:

import org.apache.log4j.*;
public class SimpleLayoutExample{
static Logger logger = 
  Logger.getLogger
("SimpleLayoutExample.class")
  public static void main (String[] args) {
  try
  {
  FileAppender fileappender = 
 
new FileAppender(new SimpleLayout(),"SimpleLog.log");
  logger.addAppender(fileappender);
  logger.info("First Log");  
  logger.info("Second Log");  
  logger.info("Third Log");  
  logger.info("Fourth Log");  
  logger.info("See your SimpleLog.log");
  logger.info("Exiting from the Main method  ");
  }catch(Exception e){
  e.printStackTrace();
  }  
  }
}

After creating and executing this SimpleLayoutExample class file you will get SimpleLog.log file with the following Log event messages.

Output:

Download Source Code

     

Related Tags for Use of SimpleLayout in Log4j:
cloggingclassormlayoutformioclassesformatoutputdisplayvithislogsimpleforieexampletoexamsseedessectionputsplimmaninrmasmoutplayoggoggispclesemalldescribexaxampsspdisplayingginatanyplayingisirhallmplpregoarsimsimplestscrssrithavsthatdispesiesimanyisplayingpleplprono


More Tutorials from this section

Ask Questions?    Discuss: Use of SimpleLayout in Log4j  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.