• Logging is very important part of programming. Logging helps programmer to understand process flow and identify the problems where actually occurred.
  • Log4J will be configured externally using properties file. We can print the logging statements in the console or we can push them in to a log file based on the requirement.
  •  org.apache.log4j class will provide required classes to implement logging
  • We need to add Log4J dependency in our project.
  • Create instance of logger by using Logger.getLogger(Log4JExample.class);
  • Lets see how to create log4j.properties file in eclipse

1. Create a maven project and add Log4J dependency:

how to create log4j.properties file in eclipse

2. Create log4j.properties file

log4j.properties example file

  1. log4j.rootLogger=INFO, console

  2. log4j.appender.console=org.apache.log4j.ConsoleAppender

  3. log4j.appender.console.layout=org.apache.log4j.PatternLayout
  4. log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS zzz}

3.Create java example program to read log4j.properties file

  1. import org.apache.log4j.BasicConfigurator;
  2. import org.apache.log4j.Logger;

  3. public class Log4JExample {

  4. static Logger logger = Logger.getLogger(Log4JExample.class);
  5.     public static void main(String[] args)
  6.     {
  7.     BasicConfigurator.configure();
  8.     logger.info("main method start!!");
  10.     System.out.println("hi");
  11.     logger.info("log4j properties configuration example");
  13.     logger.info("main method end!!");
  14.     }
  15. }


  1. 2018-02-08 23:09:10.747 IST0 [main] INFO Log4JExample  - main method start!!
  2. hi
  3. 2018-02-08 23:09:10.752 IST5 [main] INFO Log4JExample  - log4j properties configuration example
  4. 2018-02-08 23:09:10.753 IST6 [main] INFO Log4JExample  - main method end!!

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
Newer Post
Older Post

No comments

Leave a Reply

Select Menu