log4j - Sample Program



We have seen how to create a configuration file in log4j - Configuration. This chapter describe how to generate debug messages and log them in a simple text file.

Following is a simple configuration file created for our example. Let us revise it once again:

  • The appender CONSOLE is defined with type Console and writes to console.

  • The layout pattern defined is %p - %m%n, which means the printed logging message will be followed by a newline character.

  • The appender MAIN is defined with type File and writes to a file named main.log located in the logs directory.

  • The layout type defined is JsonTemplateLayout, which means the printed logging message will be in JSON format.

  • The level of the root logger is defined as DEBUG and attaches appenders named CONSOLE and MAIN with corresponding levels to it.

The contents of log4j2.properties file are as follows −

log4j2.properties

appender.0.type = Console
appender.0.name = CONSOLE
appender.0.layout.type = PatternLayout
appender.0.layout.pattern = %p - %m%n

appender.1.type = File
appender.1.name = MAIN
appender.1.fileName = logs/main.log
appender.1.layout.type = JsonTemplateLayout

rootLogger.level = DEBUG
rootLogger.appenderRef.0.ref = CONSOLE
rootLogger.appenderRef.0.level = WARN
rootLogger.appenderRef.1.ref = MAIN
rootLogger.appenderRef.0.level = INFO

Using log4j in Java Program

The following Java class is a very simple example that initializes, and then uses, the log4j logging library for Java applications.

Log4jDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.sql.SQLException;

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

public class Log4jDemo{

   /* Get actual class name by default to be printed on */
   private static final Logger LOGGER = LogManager.getLogger();

   public static void main(String[] args)throws IOException,SQLException{
      LOGGER.info("Hello this is a info message");
      LOGGER.debug("Hello this is an debug message");
      LOGGER.warn("Hello this is a warning message");
   }
}

Compile and Execute

Here are the steps to compile and run the above-mentioned program. Make sure you have set PATH and CLASSPATH appropriately before proceeding for the compilation and execution.

All the libraries should be available in CLASSPATH and your log4j2.properties file should be available in PATH. Follow the steps give below −

  • Create log4j2.properties as shown above.

  • Create Log4jDemo.java as shown above and compile it.

  • Execute Log4jDemo binary to run the program.

Output

You would get the following result inside logs/main.log file −

{"@timestamp":"2025-09-04T10:36:40.322Z","ecs.version":"1.2.0","log.level":"INFO","message":"Hello this is a info message","process.thread.name":"main","log.logger":"com.tutorialspoint.Log4jDemo"}
{"@timestamp":"2025-09-04T10:36:40.325Z","ecs.version":"1.2.0","log.level":"DEBUG","message":"Hello this is an debug message","process.thread.name":"main","log.logger":"com.tutorialspoint.Log4jDemo"}
{"@timestamp":"2025-09-04T10:36:40.325Z","ecs.version":"1.2.0","log.level":"WARN","message":"Hello this is a warning message","process.thread.name":"main","log.logger":"com.tutorialspoint.Log4jDemo"}
And following message will be printed on console −
INFO - Hello this is a info message
WARN - Hello this is a warning message
Advertisements