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.

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 !!
kapil
can you give one example of monitorInterval in log4j2.properties
praveen
hi ,
how to disable rootLogger in log4j2 ?
Lokesh Gupta
You can set
additivity
tofalse
so that the log message will only go to the named logger, and not to the root logger.nlf
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….
Hari Prasad
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.
Hitesh Kumar
How to use external properties file
Yugandhar
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.
Robin
What happens if there are more than one log4j2.properties file in the classpath?
Lokesh Gupta
There is a detailed lookup order listed here : http://logging.apache.org/log4j/2.x/manual/configuration.html
Java User
What if i want to refer log4j2.properties file from local file system and not from class path
tushar banne
only error level log is printed in console.
also there is no log in the file created.
siddheshwaran muthusamy
i also tried but file not created
howtodoinjavaReader
What do the following lines, copied from your log4j2.properties example above,, do?
status = error
name = PropertiesConfig