HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Log4j2 / Log4j2 properties file example

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.

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

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.policies.size.size=10MB
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 path

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. log4j2 properties file conexample

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) 
	[classes/:?]

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. kapil

    December 17, 2019

    can you give one example of monitorInterval in log4j2.properties

  2. praveen

    November 25, 2019

    hi ,
    how to disable rootLogger in log4j2 ?

    • Lokesh Gupta

      November 25, 2019

      You can set additivity to false so that the log message will only go to the named logger, and not to the root logger.

  3. nlf

    June 19, 2019

    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….

  4. Hari Prasad

    May 15, 2019

    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.

  5. Hitesh Kumar

    December 17, 2018

    How to use external properties file

  6. Yugandhar

    March 20, 2018

    I am trying to log in Apache camel project. I used the same above dependencies and properties. I am getting below warnngs.

    log4j:WARN No appenders could be found for logger (org.apache.camel.util.LRUCacheFactory).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

  7. Robin

    June 30, 2017

    What happens if there are more than one log4j2.properties file in the classpath?

    • Lokesh Gupta

      June 30, 2017

      There is a detailed lookup order listed here : http://logging.apache.org/log4j/2.x/manual/configuration.html

    • Java User

      December 21, 2017

      What if i want to refer log4j2.properties file from local file system and not from class path

  8. tushar banne

    May 2, 2017

    only error level log is printed in console.
    also there is no log in the file created.

    • siddheshwaran muthusamy

      April 16, 2019

      i also tried but file not created

  9. howtodoinjavaReader

    January 19, 2017

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

    status = error
    name = PropertiesConfig

Comments are closed on this article!

Search Tutorials

Log4j2 Tutorial

  • Log4j2 – Introduction
  • Log4j2 – JSON Config
  • Log4j2 – Properties Config
  • Log4j2 – XML Config
  • Log4j2 – RollingFileAppender
  • Log4j2 – Multiple appenders
  • Log4j2 – LevelRangeFilter
  • Log4j2 – HTMLLayout
  • Log4j2 – Fish Tagging
  • Log4j2 – Conversion Patterns
  • Log4j2 – JUnit

Log4j Tutorial

  • Log4j – Introduction
  • Log4j – Properties Config
  • Log4j – XML Config
  • Log4j – Maven Config
  • Log4j – Logging Levels
  • Log4j – ConsoleAppender
  • Log4j – RollingFileAppender
  • Log4j – SocketAppender
  • Log4j – JDBCAppender
  • Log4j – XMLLayout
  • Log4j – HTMLLayout
  • Log4j – Runtime Reload
  • Log4j vs. SLF4j
  • Log4j – RESTEasy + Tomcat 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces