Showing posts with label Create logs in XML format. Show all posts
Showing posts with label Create logs in XML format. Show all posts

Thursday, January 8, 2015

How to create logs in XML format 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, XML

# Define the file appender
log4j.appender.XML=org.apache.log4j.FileAppender
log4j.appender.XML.File=application.xml

# Define the xml layout for file appender
log4j.appender.XML.layout=org.apache.log4j.xml.XMLLayout
log4j.appender.XML.layout.LocationInfo=true
log4j.appender.XML.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_Xml
{
    static Logger log = Logger.getLogger(Log4j_and_Xml.class);

    public static void main(String[] args)
    {
        log.debug("This is debug message");
        log.info("This is info message");
        log.error("This is error message");
        log.fatal("This is  fatal message");
    }
}

Refresh the project to find the application.xml file
That's it, run the program and all logs will be created in XML format