Thursday, May 31, 2012

Pattern Descriptions in Log4j


Please follow the following program to understand the various letters used for pattern in log4j.

import java.io.IOException;

import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

public class PatternDescriptions {

      private static final Logger logger = Logger.getLogger(PatternDescriptions.class);

      public static void main(String[] args) throws IOException {
            Layout layout = new PatternLayout("%c %C %d %F %l %L %m %M %p %t %n");
            //c --> Used to output the category of the logging event.
            //C --> Used to output the fully qualified class name of the caller.
            //d --> Used to output the date of the logging event.
            //F --> Used to output the file name where the logging request was issued.
            //l --> Used to output location information of the caller which generated the logging event.
            //L --> Used to output the line number from where the logging request was issued.
            //m --> Used to output the application supplied message associated with the logging event.
            //M --> Used to output the method name where the logging request was issued.
            //p --> Used to output the priority of the logging event.
            //t --> Used to output the name of the thread that generated the logging event.
            //n --> Outputs the platform dependent line separator character or characters.
            Appender appender = new ConsoleAppender(layout);
            logger.addAppender(appender);
            logger.trace("Trace Message.");
            logger.debug("Debug Message.");
            logger.info("Info Message.");
            logger.warn("Warning Message.");
            logger.error("Error Message.");
            logger.fatal("Fatal Message.");
      }
}

No comments:

Post a Comment