# LOG4J : Example in Java

In 
Published 2022-12-03

This tutorial presents to you a LOG4J example in Java. This tutorial is using Eclipse and Maven. The application is a simple Java application. I suppose we have created a Java application using Maven and log4j framework is configured.

Now all we have to do is to code and to test our example of using log4j framework. For this I will create 2 java classes:

Car.java
import org.apache.log4j.Logger;
 
public class Car {
 
    private String color; 
    private int age;
    private String type;
     
    final Logger logger = Logger.getLogger(Car.class);
     
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        logger.info("The age of the Car is set !");
        this.age = age;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
}
RunTest.java
import org.apache.log4j.Logger;
 
public class RunTest {
 
    public static void main(String [] args) {
        Car car = new Car();
        car.setColor("green");
        car.setAge(4);
        car.setType("Peugeot");
 
        final Logger logger = Logger.getLogger(RunTest.class);
         
        logger.debug("This is my DEBUG text ");
        logger.error("This is my ERROR text ");
        logger.info("This is my INFO text ");
         
        System.out.println("This car is "+car.getColor()+".");
        System.out.println("This car has "+car.getAge()+" years old.");
        System.out.println("This car is a "+car.getType()+".");
    }
}

And here is the result of the log4j example into the "stdout":

2017-04-25 18:16:38 INFO Car:21 - The age of the Car is set !
2017-04-25 18:16:38 ERROR RunTest:14 - This is my ERROR text
2017-04-25 18:16:38 INFO RunTest:15 - This is my INFO text
This car is green.
This car has 4 years old.
This car is a Peugeot.

In log4j.properties file we have:

# Root logger option
log4j.rootLogger=INFO, stdout, file

# Log the messages to the console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Log the messages to a log file with file rolling
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\JAVA\\log4j-application.log
log4j.appender.file.MaxFileSize=4MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Here are some things to retain:

  • When you log the information (with the standard Layout) you will see the object class which write the log ("Car", "RunTest").

  • When you log the information (with the standard Layout) you will see the line within the class where the log is generated (Car:21 -> the line is 21).

  • System.out.println is writing only to the console and not to the log file.