Spring boot log4j2.xml example

Spring boot’s default logging uses Logback which is included as transitive dependency. Spring Boot supports Log4j2 for logging configuration if it is on the classpath.

To configure Log4j2 with Spring Boot2, follow these two simple steps:

1. Log4j2 Maven Dependency

In fact, it is two steps change. First remove spring’s default logging dependency and then add log4j2 dependency.




2. Add log4j2.xml file in resources folder

Spring will automatically configure log4j2 once it finds it’s jar files in classpath. Now you have to add log4j2.xml or (log4j2.properties) in src/main/resources folder. That’s all.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
		<Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
		<Property name="APP_LOG_ROOT">c:/temp</Property>
		<Console name="Console" target="SYSTEM_OUT" follow="true">
			<PatternLayout pattern="${LOG_PATTERN}" />

		<RollingFile name="appLog"
			<PatternLayout pattern="${LOG_PATTERN}" />
				<SizeBasedTriggeringPolicy size="19500KB" />
			<DefaultRolloverStrategy max="1" />


		<Logger name="com.howtodoinjava.app" additivity="false">
			<AppenderRef ref="appLog" />
			<AppenderRef ref="Console" />

		<Root level="debug">
			<AppenderRef ref="Console" />

3. Spring boot log4j2 demo

Now add few log statements in application and see the logs in configured appenders e.g. I configured console and file.

package com.howtodoinjava.app;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;

public class Application extends SpringBootServletInitializer {

	private static final Logger LOGGER = LogManager.getLogger(Application.class);

	public static void main(String[] args)
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
        LOGGER.info("Info level log message");
        LOGGER.debug("Debug level log message");
        LOGGER.error("Error level log message");

Check the output.

2018-06-01T13:55:42.506+0530 INFO Info level log message
2018-06-01T13:55:42.506+0530 DEBUG Debug level log message
2018-06-01T13:55:42.506+0530 ERROR Error level log message

Let me know if you face any problem in this spring boot log4j2 example.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

7 thoughts on “Spring boot log4j2.xml example”

  1. I am getting this problem and in the console and Info and error log is showing on console but debug line is not printing on console

    SLF4J: Class path contains multiple SLF4J bindings.
    SLF4J: Found binding in [jar:file:/C:/Users/1707393/.m2/repository/ ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
    SLF4J: Found binding in [jar:file:/C:/Users/1707393/.m2/repository/ org/apache/logging/log4j/log4j-slf4j-impl/2.12.1/log4j-slf4j-impl-2.12.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
    SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
    SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
    • In Springboot the default logging level is INFO which means info and higher logging levels be printed, like ERROR, WARN, FATAL etc but lower levels like DEBUG and TRACE wont be printed. Change the logging level to see DEBUG logs

  2. Hi Getting below error while starting boot app

    ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console…






Leave a Comment


A blog about Java and related technologies, the best practices, algorithms, and interview questions.