Saturday, January 3, 2015

How to create logs in HTML formats using log4j

1)Create a project in Eclipse
2)Create a file - log4j.properties and paste the following content in it

# Define the root logger with file appender
log4j.rootLogger = DEBUG, HTML

# Define the file appender
log4j.appender.HTML=org.apache.log4j.FileAppender
log4j.appender.HTML.File=application.html

# Define the html layout for file appender
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
log4j.appender.HTML.layout.Title=Application logs
log4j.appender.HTML.layout.LocationInfo=true
log4j.appender.HTML.Threshold=DEBUG

3)Create a sample class to log
(Make sure log4j jar file is added in the build path)

import org.apache.log4j.Logger;

public class log4j_and_html
{
    static Logger log = Logger.getLogger(log4j_and_html.class);

    public static void main(String[] args)
    {

        log.debug("Sample debug message");
        log.info("Sample info message");
        log.error("Sample error message");
        log.fatal("Sample fatal message");
    }
}

That's it, run the program and all logs will be created in HTML format


No comments:

Post a Comment