Print Logs Output as JSON String in Spring Boot Java — Log4j2


You already have spring boot project. If you don’t, take the tutorial —

If you have spring boot project, your pom.xml would be something like the following:

Spring boot comes default packaged with logging. Refer here —

The format of the default log is:

You want JSON log out put.

We will achieve that using log4j2.

Step 1:

Include the log4j2 spring boot dependency and exclude the any version conflicts of including common libs at multiple places.

Step 2:

If you have been using any other logs please check and remove those also using exclusions as above. Check dependencies using the following and remove them:

Step 3:

Create a file with name log4j2-spring.xml with the following content and add to your resources folder.

You have to replace the italics. These are the pattern of the log and your packages where you want your logger to be available.

Step 4:

Use your logger.

The output would be something like the following:

Learning the art of learning, embracing, being reviewed, empathizing, reviewing…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store