Spring boot logging log4j2 example

What you will learn here about Spring boot logging

  • spring boot logging log4j2 example
    1. spring boot log4j2 dependency
    2. spring boot log4j2 properties
    3. Spring boot logging in file

It is very easy to add logging functionality in spring boot. Here we are going to see how to add logging functionality in spring boot using log4j. Please click on below link to download the sample project of logging in spring boot using log4j.
Download spring boot logging log4j2 example project (449 downloads)

Spring boot logging log4j2 example

Please follow the following steps to know how to add log in spring boot application using log4j2.
1)Add logging dependency in pom.xml file which is shown below. you will get dependencies in above sample project
spring boot log4j2 dependency

2)Create properties file and add configurations

  1. First create log4j2.properties file under resources folder which is shown below
  2. Add properties in log4j2.properties file which you will get in above sample project
  3. Please replace logger.file.name with the your application root package which is shown below

spring boot log4j2 properties

3)Create rest controller and start logging in file

  1. First create new package and then create new class which is shown below in step 1
  2. Create logger Object which is shown below in step2
  3. Start logging using logger object which is shown below in step 3

spring boot logging log4j2 example

4)Run the spring application

  1. Open your Spring boot application
  2. Run the application

run Spring boot application

5)Make Get request from browser and check the logs

  1. Open the browser and make get Request for /home URL which is shown in step 1
  2. Now please go to eclipse and right click on project and please click on Refresh which is done in step 2
  3. Now you should able to see logs folder which is shown in step 3. If you are able to see means you are able to logging logs using log4j successfully.

Spring boot logging

You may also like...