Layouts in Log4j

The output of an appender can be customized by associating with it a Layout. These are the types of Layout available:

Simple layout-- org.apache.log4j.SimpleLayout
SimpleLayout formats the output in a very simple manner, it prints the Level, then a dash '-' and then the log message.

PatternLayout --org.apache.log4j.PatternLayout
PatternLayout formats the output based on a conversion pattern specified, or if none is specified, the default conversion pattern.

HTML layout-- org.apache.log4j.HTMLLayout
HTMLLayout formats the output as a HTML table.

XMLLayout-- org.apache.log4j.xml.XMLLayout
XMLLayout formats the output as a XML.


Illustrating Examples

SimpleLayout and FileAppender
Here is a very simplistic example of a program implementing a SimpleLayout and FileAppender:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.FileAppender;
public class simpandfile {
static Logger logger = Logger.getLogger(simpandfile.class);
public static void main(String args[]) {
SimpleLayout layout = new SimpleLayout();
FileAppender appender = null;
try {
appender = new FileAppender(layout,"output1.txt",false);
} catch(Exception e) {}


logger.addAppender(appender);
logger.setLevel((Level) Level.DEBUG);


logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}


HTMLLayout and WriterAppender
Here is a very simplistic example of a program implementing a HTMLLayout and WriterAppender:

import java.io.*;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.HTMLLayout;
import org.apache.log4j.WriterAppender;
public class htmlandwrite {
static Logger logger = Logger.getLogger(htmlandwrite.class);
public static void main(String args[]) {
HTMLLayout layout = new HTMLLayout();

WriterAppender appender = null;
try {
FileOutputStream output = new FileOutputStream("output2.html");
appender = new WriterAppender(layout,output);
} catch(Exception e) {}

logger.addAppender(appender);
logger.setLevel((Level) Level.DEBUG);

logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}


PatternLayout and ConsoleAppender
Here is a very simplistic example of a program implementing a PatternLayout and ConsoleAppender:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.ConsoleAppender;
public class consandpatt {
static Logger logger = Logger.getLogger(consandpatt.class);
public static void main(String args[]) {

// Note, %n is newline
String pattern = "Milliseconds since program start: %r %n";
pattern += "Classname of caller: %C %n";
pattern += "Date in ISO8601 format: %d{ISO8601} %n";
pattern += "Location of log event: %l %n";
pattern += "Message: %m %n %n";

PatternLayout layout = new PatternLayout(pattern);
ConsoleAppender appender = new ConsoleAppender(layout);

logger.addAppender(appender);
logger.setLevel((Level) Level.DEBUG);

logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}


Substitute symbol
%c Logger, %c{2 } last 2 partial names
%C Class name (full agony), %C{2 } last 2 partial names
%d{dd MMM yyyy HH:MM:ss } Date, format see java.text.SimpleDateFormat
%F File name
%l Location (caution: compiler-option-dependently)
%L Line number
%m user-defined message
%M Method name
%p Level
%r Milliseconds since program start
%t Threadname
%x, %X see Doku
%% individual percentage sign
Caution: %C, %F, %l, %L, %M slow down program run!

0 comments: