Friday, 7 August 2015

Create logs in html format using log4j (Core JAVA)

There are two steps to create HTML log file:
1. Create log4j.properties
2. Create Log4jHtmlLayout.java (this is just java file specifies the usage of log4j)

If you want only customize logger then change "log4j.rootLogger = All, FILE, rfile" to "log4j.rootLogger = INFO, FILE, rfile"

log4j.properties


# Define the root logger with appender file
log4j.rootLogger = All, FILE, rfile
 

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.logfile.File=./TestLogs/Logfile.log

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.File=./TestLogs/Logfile.log
log4j.appender.FILE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# Define the layout for RollingFileAppender
log4j.appender.rfile=org.apache.log4j.RollingFileAppender
log4j.appender.rfile.File=./TestLogs/applog.html
log4j.appender.rfile.MaxFileSize=1000MB
log4j.appender.rfile.Append=true
log4j.appender.rfile.layout=org.apache.log4j.HTMLLayout


Log4jHtmlLayout.java


package testing.MavenTestNG;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Log4jHtmlLayout {

 static Logger log = Logger.getLogger(Log4jHtmlLayout.class);
 
 public static void main(String[] args) {
  
  PropertyConfigurator.configure("config/log4j.properties");
  log.debug("Sample debug message");
        log.info("Sample info message");
        log.error("Sample error message");
     }

}

Below is the example HTML log file (applog.html-as specified in log4j.properties)



No comments:

Post a Comment