Kotlin SpringBoot configure Apache Log4j2

Apache Log4j2 is an upgrade to Log4j that provides significant improvements over its predecessor, Log4j 1.x, and provides many of the improvements available in Logback while fixing some inherent problems in Logback’s architecture. The tutorial will introduce way to configure Apache Log4j2 with Kotlin SpringBoot.

I. Technologies

– Java 1.8
– Maven 3.6.1
– Spring Tool Suite – Version 3.9.0.RELEASE
– Spring Boot – 1.5.9.RELEASE
– Kotlin

II. Practice

We use SpringToolSuite to create a Kotlin SpringBoot project as below structure:

Kotlin SpringBoot configure Apache Log4j2 - project structure

Step to do:
– Configure SpringBoot Log4j2 dependencies
– Create a Simple Controller
– Configure Log4j2.xml
– Build & Run

1. Configure SpringBoot Log4j2 dependencies

– Exclude logback from default log dependency of SpringBoot:

– Add Spring Web MVC dependency:

– Add Log4j2 dependency

2. Create a Simple Controller

We define a logger which is ready for showing messages with 4 mode: Debug, Info, Warn, Error

3. Configure Log4j2.xml

– Create a log4j2.xml file for configuration log4j2 (Spring will understand that the “log4j2.xml” file is a configuration for log4j2).

– Create 3 Loggers:

– With 3 Appenders:

– Full Details:

4. Build & Run

Run the SpringBoot project as a commandline mvn spring-boot:run.
Then make a request http://localhost:8080/:
-> Console logs:

Note: Because levellog = INFO so DEBUG log is missed.

-> Log files:

Kotlin SpringBoot configure Apache Log4j2 - results

  • app_log.log
  • springboot_log.log
  • III. Sourcecode

    SpringBootKotlinLog4J2

    By JavaSampleApproach | December 6, 2017.


    Related Posts


    Got Something To Say:

    Your email address will not be published. Required fields are marked *

    *