Log4j Layouts

Logging can have below mentioned layouts:

HTMLLayout

log4j.properties:

# Root logger option
log4j.rootLogger=INFO, html
 
# Direct log messages to a log file
log4j.appender.html=org.apache.log4j.FileAppender
log4j.appender.html.File=D:\logs\logging.html
log4j.appender.html.layout=org.apache.log4j.HTMLLayout

Java File:

import org.apache.log4j.Logger;

public class LoggingExample {

	private static Logger log = 
	        Logger.getLogger(LoggingExample.class);

	public static void main(String[] args) {
		
		log.trace("Trace Log Message");
		log.debug("Debug Log Message");
		log.info("Info Log Message");
		log.warn("Warn Log Message");
		log.error("Error Log Message");
		log.fatal("Fatal Log Message");
	}

}

Output:

Log4j

PatternLayout:

log4j.properties:

# Root logger option
log4j.rootLogger=INFO, file
 
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\logs\logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=0
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=
          %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

%d{yyyy-MM-dd HH:mm:ss} for date and time

%-5p representing the log level

%c{1} representing the file name

:%L representing the line number of java file

%m to display the logging message

%n representing the logger messages line by line

Output:

Log4j

SimpleLayout

log4j.properties:

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout

Output:

INFO - Info Log Message
WARN - Warn Log Message
ERROR - Error Log Message
FATAL - Fatal Log Message

XMLLayout

log4j.properties:

# Root logger option
log4j.rootLogger=INFO, file
 
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\logs\logging.xml
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.layout=org.apache.log4j.xml.XMLLayout

Output: logging.xml file-

<log4j:event logger="LoggingExample" timestamp="1417752802054" 
                                   level="INFO" thread="main">
<log4j:message><![CDATA[Info Log Message]]></log4j:message>
</log4j:event>

<log4j:event logger="LoggingExample" timestamp="1417752802057" 
                                    level="WARN" thread="main">
<log4j:message><![CDATA[Warn Log Message]]></log4j:message>
</log4j:event>

<log4j:event logger="LoggingExample" timestamp="1417752802057" 
                                   level="ERROR" thread="main">
<log4j:message><![CDATA[Error Log Message]]></log4j:message>
</log4j:event>

<log4j:event logger="LoggingExample" timestamp="1417752802058" 
                                    level="FATAL" thread="main">
<log4j:message><![CDATA[Fatal Log Message]]></log4j:message>
</log4j:event>