Log4j2 Properties File Example

Learn to configure log4j2.properties file to output the log statements to console, rolling files etc. Learn to configure log4j2 appenders, levels and patterns.

Apache Log4j2 is an upgrade to Log4j 1.x that provides significant improvements over its predecessor such as performance improvement, automatic reloading of modified configuration files, Java 8 lambda support and custom log levels.

1. Log4j2 Maven Dependencies

To include Log4j2, include below maven dependency in the project.


2. Log4j2.properties – Console Logging

We can use below log4j2.properties file logging output into console. Please note that if no configuration file could be located then DefaultConfiguration will be used. Log4j2 default logging also causes logging output to go to the console.

status = error
name = PropertiesConfig

filters = threshold

filter.threshold.type = ThresholdFilter
filter.threshold.level = debug

appenders = 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} %-5p %c{1}:%L - %m%n

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

3. Log4j2.properties – Rolling File Appender

We can use below log4j2.properties file logging output into date based rolling files.

status = error
name = PropertiesConfig

#Make sure to change log file path as per your need
property.filename = C:\\logs\\debug.log

filters = threshold

filter.threshold.type = ThresholdFilter
filter.threshold.level = debug

appenders = rolling

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20

loggers = rolling

#Make sure to change the package structure as per your application

logger.rolling.name = com.howtodoinjava
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

4. Log4j2.properties File Location

We should put log4j2.properties anywhere in application’s classpath. Log4j2 will scan all classpath locations to find out this file and then load it.

Log4j2.properties file location
Log4j2.properties file location

5. Demo

Let’s write a java class and write few log statements to verify that logs are appearing in console and log file as well.

package com.howtodoinjava.log4j2.examples;

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

public class Log4j2HelloWorldExample 
	private static final Logger LOGGER = LogManager.getLogger(Log4j2HelloWorldExample.class.getName());
	public static void main(String[] args) 
		LOGGER.debug("Debug Message Logged !!!");
		LOGGER.info("Info Message Logged !!!");
		LOGGER.error("Error Message Logged !!!", new NullPointerException("NullError"));

Now when you run the above program, you will get below logs in console.

2016-06-16 13:41:27 DEBUG Log4j2HelloWorldExample:12 - Debug Message Logged !!!
2016-06-16 13:41:27 INFO  Log4j2HelloWorldExample:13 - Info Message Logged !!!
2016-06-16 13:41:27 ERROR Log4j2HelloWorldExample:14 - Error Message Logged !!!
java.lang.NullPointerException: NullError
	at com.howtodoinjava.log4j2.examples.Log4j2HelloWorldExample.main(Log4j2HelloWorldExample.java:14) 

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

13 thoughts on “Log4j2 Properties File Example”

  1. This is the first example that actually got something to log on the console for me!!! Although not sure what any of it means….most documention on apache log4j is old and not update. Thanks for this tutorial. Off to go read more….

  2. Tried to log details into the file using the rolling file property content showed in the explanation. But only the last log message is available in the file.

    I my example I logged 3 debug message – 1. successful login 2. Verified with incorrect data and 3. verified with null data.

    Only verified with null data is displayed in my log file. But I can see all the messages in the console.

  3. What do the following lines, copied from your log4j2.properties example above,, do?

    status = error
    name = PropertiesConfig


Leave a Comment


A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.