How to use Log4J 2 Configuration: Using Properties File

1. What is log4j2 ?

Apache Log4j2 is a logging framework for Java. It is a successor to Log4j, and it provides a number of improvements over its predecessor, including:
  • Improved performance
  • More flexible configuration
  • Support for multiple logging levels
  • Support for multiple logging appenders
Log4j2 is widely used in Java applications, and it is considered to be one of the most popular logging frameworks available. In December 2021, a critical vulnerability was discovered in Log4j2. This vulnerability, known as Log4Shell, could allow attackers to execute arbitrary code on vulnerable systems. The vulnerability was quickly patched, but it caused widespread disruption and damage. Despite the vulnerability, Log4j2 remains a popular logging framework. It is important to keep your Log4j2 installation up to date to ensure that you are protected from known vulnerabilities. Log4j2 is use by many programming languages like
  • Java
  • Scala
  • .Net
  • Kotlin
XML, JSON, YAML, and other configuration files that are located on your project’s classpath can be used to configure Log4J 2 as well as programmatically in your application. You have the freedom to change the various configuration options using configuration files without changing the application code. We’ll discuss using the properties file in this article.

2. Java Code to use Log4j2

package org.bigtechtalk.log4jdemo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Loj4j2PropertyFileDemo
{
   static Logger logger = LogManager.getLogger(Loj4j2PropertyFileDemo.class);
   public static void main( String[] args )
   {
       logger.debug("Debug Message Logged !!!");
       logger.info("Info Message Logged !!!");
       logger.error("Error Message Logged !!!", new NullPointerException("NullError"));
   }
}

3. Maven Dependency

To use log4j2 in your maven project you need to add the dependency below in your pom.xml file.
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.19.0</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.19.0</version>
</dependency>

4. Log4J 2 Properties File

By default, Log4J 2 looks for a properties file with the name log4j2.properties in the classpath.

4.1 Config file to write log to console

First we will start with writing the log messages to the console. So we will define our log4j2.properties file like below.

name= BigTechTalk Java Logging
appends = console

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] %c{1}- %msg%n

# Configure root logger
rootLogger.level = debug
rootLogger.appenderRef.stdout.ref = STDOUT

In the log4j2.properties files we have defined an appender to write the message to the console.
I have set the rootLogger level as debug and stdout.ref is the name of appender.console.name

4.2 Config file to write log to a file

To write the log messages to the file we need to define the basePath which is log directory location and fileName
name= BigTechTalk Java Logging
appends = file
# Log files location
property.basePath = c:/temp/logs

# RollingFileAppender name, pattern, path and rollover policy
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName= ${basePath}/loggerFile.log
appender.rolling.filePattern=${basePath}/loggerFile_%d{yyyyMMdd}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] %c{1}- %msg%n
appender.rolling.policies.type = Policies

# Configure root logger
rootLogger.level = debug
rootLogger.appenderRef.rolling.ref = fileLogger

In the above config file we have used ${basePath} which acts as a substitution of property.basePath and appender.rolling.filename to specify the log file name.
Since we are using appender.rolling then we have to define the appender.rolling.layout.pattern and appender.rolling.policies.type.

4.3 Config file to write log to a file and Console

The below log4j2.properties file can be used to write the log message to file and console simultaneously.

name= BigTechTalk Java Logging
appends = console, file
# Log files location
property.basePath = c:/temp/logs
#Console Output
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] %c{1}- %msg%n

# RollingFileAppender name, pattern, path and rollover policy
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName= ${basePath}/app.log
appender.rolling.filePattern= ${basePath}/app_%d{yyyyMMdd}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] %c{1}- %msg%n
appender.rolling.policies.type = Policies

# RollingFileAppender rotation policy
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size = 10MB
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true

# Configure root logger
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger.appenderRef.rolling.ref = fileLogger

We utilized two appenders in the above config file, i.e appender.console and appender.rolling .In contrast to appender.rolling, which is used to write logs to files, appender.console is used to write logs to the console.


We’ve even built up policies for appender.rolling that make it advantageous to use appender.rolling.filePattern to compress the old log file.

Additionally, the pattern can assist in removing files based on size (appender.rolling.policies.size.size) and logs based on time (appender.rolling.policies.time.type). appender.rolling.strategy.delete.ifLastModified.age can be used to delete logs which are older than 30d (days)

5. Conclusion

This article shows very simple examples to use log4j2.property file in java code. Code for used in his article can be found at Github