Spring Boot Logging with Lombok

Project Lombok is very handy tool for removing the boilerplate code from application. Lombok can also be used to configure logging in spring boot applications and thus removing the boilerplate code for getting the logger instance.

1. Import lombok in spring boot application

Before using it, we must import the lombok in spring boot application. Don’t forget to install lombok into eclipse before using it.

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
	http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.company</groupId>
	<artifactId>SpringBootEhcache</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>SpringBootEhcache</name>
	<url>http://maven.apache.org</url>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath />
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<skipTests>true</skipTests>
	</properties>

	<dependencies>
	
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

2. @Log, @Log4j2, @Slf4j – Lombok annotations

Instead, we can start writing log statements in a java class which is annotated with lombok’s @Log annotations. Lombok supports following log annotations for spring boot –

  • @CommonsLog – Creates the logger with following statement:
    private static final org.apache.commons.logging.Log log = 
        org.apache.commons.logging.LogFactory.getLog(LogExample.class);
  • @Log – Creates the logger with following statement:
    private static final java.util.logging.Logger log = 
        java.util.logging.Logger.getLogger(LogExample.class.getName());
  • @Log4j2 – Creates the logger with following statement:
    private static final org.apache.logging.log4j.Logger log = 
        org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
  • @Slf4j – Creates the logger with following statement:
    Creates private static final org.slf4j.Logger log = 
        org.slf4j.LoggerFactory.getLogger(LogExample.class);

A sample usage of this log annotation is :

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootApplication
public class Application 
{
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
		
		log.info("Simple log statement with inputs {}, {} and {}", 1, 2, 3);
	}
}

By default, spring boot uses logback as logging provider.

3. Using Log4j2 Logging with Lombok

To use log4j2, exclude logback from classpath and include log4j2.

<dependencies>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
		<exclusions>
			<exclusion>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-logging</artifactId>
			</exclusion>
		</exclusions>
	</dependency>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-log4j2</artifactId>
	</dependency>

	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
	</dependency>

</dependencies>

Now you can use either @Slf4j (recommneded) or @Log4j2 at class to use log4j2 as underlying logging implementation.

import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootApplication
public class Application 
{
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
		
		log.info("Simple log statement with inputs {}, {} and {}", 1, 2, 3);
	}
}
2019-07-28 15:29:41.267  INFO 7660 --- [main] com.howtodoinjava.demo.Application       
	: Simple log statement with inputs 1, 2 and 3

Drop me your questions related to using lombok for spring boot logging.

Happy Learning !!

References:

Lombok logging annotations

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

9 thoughts on “Spring Boot Logging with Lombok”

  1. Thank you for the nice overview of logging in Spring Boot. Could you let us know about your opinion on log4j vs logback as the underlying loggin provider?

    Reply
    • Interestingly, one person is behind all three logging solutions (log4j, logback, and SLF4j). You will not see any difference in terms of performance because all work asynchronously and at the same level in OS.

      You can choose any of log4j2 or logback, based on your comfort level with the configuration syntax.

      Reply
  2. How to deal with test coverage and stuff like:

    if (log.isDebugEnabled()) {
       log.debug("Something!");
    }
    

    in my sonar checks I get: Branches should have sufficient coverage by tests. It means that my if is not part of the coverage.

    Regards

    Alfredo

    Reply
  3. Hi,
    Thank you for simple yet detailed explanation.

    Can you please provide information on how to configure Rolling File Appender using @Slf4j annotation?

    thanks

    Reply
  4. Hello Lokesh
    Thank you very much with your fantastic documentation!
    A simple question, but I expect it could be with a difficult answer 🙂

    I am just learning and I am not sure, but I assume slf4j is the base for both, but the logger itself is an other library.
    Which are the parameter which will help to decide if I should use “lombok.extern.slf4j.Slf4j;” or “org.slf4j.Logger;” ?
    Thank you in advance
    rgds OpaHeinz

    Reply
  5. Hi Lokesh,
    Greetings of the day. I am currently using lombok to generate logs file. Can you please help me with generating DEBUG, ERROR, INFO logs in seperate file using lombok.

    Thanks in Advance.
    Regards,
    Mohit

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.