log4j properties file example

Logging is essential part of programming. It helps developer to track code workflow and fix bugs efficiently. If we get any issue in the code, we check logs for that workflow or functionality.

Log4j is fast , reliable logging framework and can be easily integrated with the code. It is possible to enable logging at run time without using

There are two ways, you can do logging using log4j.
  1. log4j.properties
  2. log4j.xml
In this post, we will see how to configure using log4.properties

1) Create simple maven java project.

2) Put log4j entry in pom.xml .

<dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.17</version>
</dependency>

3) Update maven project to download require jar

Right click on project-> Maven -> update project

4) Create log4j.properties file

This file is main file for log4j configuration. It has information about log levels, log appenders.

Log to console:

If you want to log to console, you need to have following lines in 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=[%t] %-5p %c %x - %m%n
Log to file :

If you want to log to console, you need to have following lines in log4j.properties and also you need to provide log file name too.
log4j.rootLogger=DEBUG, console
log4j.rootLogger=DEBUG,file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
log4j.appender.file.File=applicationLogs.log

Log to console and file both :

If you want to log on both to console and file, you can use below code:
log4j.rootLogger=DEBUG, console, file

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

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
log4j.appender.file.File=applicationLogs.log

5) Sample class for Logger 

Create a class called "LoggingMain.java" in src/main/java in package org.arpit.java2blog
package org.arpit.java2blog;

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

public class LoggingMain {

  private static final Logger logger = LogManager.getLogger(LoggingMain.class);
 public static void main(String[] args) {
 
  PropertyConfigurator.configure("log4j.properties");
  logger.info("Hello world");
  logger.info("we are in logger info mode");

 }

}

6) Run program:

When you run above program , you will get below output to console and file(applicationLogs.log)
0 [main] INFO org.arpit.java2blog.LoggingMain  - Hello world
1 [main] INFO org.arpit.java2blog.LoggingMain  - we are in logger info mode
Bingo!! we have successfully configured log4j using properties file in maven project.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012