Home Tutorials Log4j Layouts in Log4j

Related Tutorials


 
 

Share on Google+Share on Google+

Layouts in Log4j

Advertisement
Users more often wish to customize their output according to their output format. Layout can be set by attaching it with the appender.

Layouts in Log4j

     

Users more often wish to customize their output according to their output format. Layout can be set by attaching it with the appender. The Layout is responsible for formatting the logging output.

There are following layout classes in Log4j:

  • PatternLayout
  • SimpleLayout
  • HTMLLayout
  • XMLLayout
  • TTCCLayout

Pattern Layout :

In our this section we are describing about most general layout PatternLayout. It is a flexible layout configurable with its pattern string. A conversion pattern consists of literal text and format control expressions. Here is the list of some of the conversion characters used for formatting:

   Character  Effect
c Used to output the category of logging event
C Used to output the fully qualified class name of the caller issuing the logging request
d Used to output date of the logging event
m Used to output the application supplied message associated with the logging event
n Outputs the platform dependent line separator character or characters

p

Used to output the priority of the logging event
r Used to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event
t Used to output name of thread of logging event
F Used to output the file name

There are two constructors in the PatternLayout class:

  • Pattern() creates pattern with the DEFAULT_LAYOUT_PATTERN
  • Pattern(String pattern) with the specified pattern string

Here is the example which uses PatternLayout class for creating output in the "%r [%t] %-5p %c %x - %m%n" pattern. Following is the source code for PatternLayoutExample.java :

import org.apache.log4j.*;
public class PatternLayoutExample {
static Logger logger = Logger.getLogger("PatternLayoutExample");
  public static void main(String[] args) {
  try
  {
  PatternLayout pattern = 
 
new PatternLayout("%r [%t] %-5p %c %x - %m%n");
  FileAppender fileappend=new FileAppender(pattern,"Output.log");
  logger.addAppender(fileappend);
  logger.info("Hello !");
  logger.info("Welcome");
  logger.debug("Exiting from main method");  
  }catch(Exception e){
  System.out.println(e.getMessage());
  }  
  }
}

After creating and executing this file there will be created an "Output.log" file and log events will be added into it. As output you will have an "Output.log" file with the following line.

0 [main] INFO PatternLayoutExample - Hello !
0 [main] INFO PatternLayoutExample - Welcome
0 [main] DEBUG PatternLayoutExample - Exiting from main method

Download Source Code

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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

Related Tutorials

Discuss: Layouts in Log4j   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com