Log4j Example using Property File

In this tutorials we are going to see how to create simple log4j application using Property Configurator with java program, previously we have seen log4j application using Basic Configurator we can obtain same output using property file.

Add Log4j jar in your class path or Log4j maven dependency in your pom.xml . This tutorial created based on log4j 1.2.17 version. Check our latest tutorials to know about Log4j 2 .

Log4j Maven dependency

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

log4j.properties

log4j.rootLogger= Debug,console

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

log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%r [%t] %p %c %x - %m%n 

rootLogger used to configure Level and Appender, in this example I used DEBUG as Level and Console appender as appender(console is name of appender).

PatternLayout is used to configure log4j message format.We will see Pattern Layout in detail in upcoming chapter.

Log4jExample.java

package com.ehowtonow.log4j.property;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Log4jPropertyExample {

 private static final String FILE_PATH = "log4j.properties";
 private static final Logger log = Logger
   .getLogger(Log4jPropertyExample.class);

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  if (FILE_PATH == null) {
   BasicConfigurator.configure();
   log.info("Log4j Configured using Basic Configurator");
  } else {
   PropertyConfigurator.configure(FILE_PATH);
   log.info("Log4j Configured using Property Configurator");
  }
  
  log.debug("Debug message by eHowToNow.com");
  log.info("Info message by eHowToNow.com");
  log.warn("Warn message by eHowToNow.com");
  log.error("Error message by eHowToNow.com");
  log.fatal("Fatal message by eHowToNow.com");
 }

}

Create instance for Logger class , getLogger(Log4jPropertyExample .class) method accept fully qualified class name as its argument. In this example if property file not exist in specified location then BasicConfigurator.configure() used to initialize log4j application otherwise PropertyConfigurator.configure(FILE_PATH) loads the configuration from property file .

now you can run the application, you will see the following output in console.

0 [main] INFO com.ehowtonow.log4j.property.Log4jPropertyExample – Log4j Configured using Property Configurator
0 [main] DEBUG com.ehowtonow.log4j.property.Log4jPropertyExample – Debug message by eHowToNow.com
0 [main] INFO com.ehowtonow.log4j.property.Log4jPropertyExample – Info message by eHowToNow.com
0 [main] WARN com.ehowtonow.log4j.property.Log4jPropertyExample – Warn message by eHowToNow.com
0 [main] ERROR com.ehowtonow.log4j.property.Log4jPropertyExample – Error message by eHowToNow.com
0 [main] FATAL com.ehowtonow.log4j.property.Log4jPropertyExample – Fatal message by eHowToNow.com


Posts You may Like

  1. Java 7 Try statement with multi catch
  2. How to Convert Object to JSON String using GSON
  3. How to send Email with Attachment using Java
  4. Java Best Practice Naming Conventions
  5. Log4j Introduction
  6. Java Best Practice – Declarations
  7. Java 7 new features
  8. Java 8 Sort Map by Value
  9. RESTful Web Services Introduction
  10. How to append content into existing file in Java
  11. How to delete directory using Java
  12. Sending Simple Email Using Java Mail API
  13. Java 8 Stream Collectors groupingBy with count and sum Example
  14. How to retrieve all the files from Directory using Java File.listFiles()
  15. Java Tutorial – Java Environment Setup in Linux Ubuntu with apt-get

Be the first to comment

Leave a Reply

Your email address will not be published.


*