Thursday, May 31, 2012

Logger with Java Configuration


Please follow the following program to implement log4j with Java Configuration and use the provided scenario's to use the specific logger methods.
import java.io.IOException;

import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

public class JavaLoggerDemo {

      private static final Logger logger = Logger.getLogger(JavaLoggerDemo.class);

      public static void main(String[] args) throws IOException {
            Layout layout = new PatternLayout("%-4r [%t] %-5p %c %m %n");
            Appender appender = new FileAppender(layout, "hello.log");
            logger.addAppender(appender);
            logger.trace("Trace Message.");
            logger.debug("Debug Message.");
            logger.info("Info Message.");
            logger.warn("Warning Message.");
            logger.error("Error Message.");
            logger.fatal("Fatal Message.");
      }
}

No comments:

Post a Comment