Saturday, August 3, 2013

Log4j example in java


  1. Download “log4j-x.x.x.jar” file.
  2. Create a java project in eclipse.
  3. Open project’s properties and click on “Java Build Path” and then go to “Libraries” tab and add this jar by click on “Add External Jars..” tab.
  4.            Now create an utils class under your defined package (For my case I used package name as “com.log4j.example” and file name as “Log4JUtils.java”. Code is as follows,
    package com.log4j.example;

    import org.apache.log4j.ConsoleAppender;
    import org.apache.log4j.Layout;
    import org.apache.log4j.Logger;
    import org.apache.log4j.PatternLayout;

    public class Log4JUtils {
          public static void configureLogger(Logger logger) {
                Layout layout = new PatternLayout(
                            "%d{yyyy-MM-dd HH:mm:ss} %-5p %x [%t] %c{2} - %m%n");
                logger.addAppender(new ConsoleAppender(layout));
          }

    }

      5.      Now create the main class. (For my case same package as before and class name as “ClientClass.java” and code is as follows,
    package com.log4j.example;

    import org.apache.log4j.Logger;

    public class ClientClass {
          public static void main(String[] args) {
               
                  final Logger logger = Logger.getLogger(ClientClass.class);
                  Log4JUtils.configureLogger(logger);
                 
                  /*You can set any one of the following level and according to your setting log message will be displayed.
                   * ALL- Display all level of message in console
                   * DEBUG- Display from this level to lower level message.
                   * INFO- Display from this level to lower level message.
                   * WARN- Display from this level to lower level message.
                   * ERROR- Display from this level to lower level message.
                   * FATAL- Display from this level to lower level message.
                   * OFF- Stop to display log message.*/
                  //logger.setLevel(Level.ALL);
                  //logger.setLevel(Level.DEBUG);
                  //logger.setLevel(Level.INFO);
                  //logger.setLevel(Level.WARN);
                  //logger.setLevel(Level.ERROR);
                  //logger.setLevel(Level.FATAL);
                  //logger.setLevel(Level.OFF);
                 
                  //Log4j levels priority [DEBUG>INFO>WARN>ERROR>FATAL]
                  logger.debug("Logging debug message ..................");
                  logger.info("Logging info message ..................");
                  logger.warn("Logging warn message ..................");
                  logger.error("Logging error message ..................");
                  logger.fatal("Logging fatal message ..................");
          }
    }



    6. Output =>
    2013-07-24 13:19:55 DEBUG  [main] example.ClientClass - Logging debug message ..................
    2013-07-24 13:19:55 INFO   [main] example.ClientClass - Logging info message ..................
    2013-07-24 13:19:55 WARN   [main] example.ClientClass - Logging warn message ..................
    2013-07-24 13:19:55 ERROR  [main] example.ClientClass - Logging error message ..................
    2013-07-24 13:19:55 FATAL  [main] example.ClientClass - Logging fatal message ..................

No comments:

Post a Comment