In this tutorial we will investigate how Lombok works with @Slf4j annotation for logging with several examples and also we will see how to use custom Logger using @CustomLog and Lombok’s configuration support for logging.
To use @Slf4j annotation in spring boot application, you can checkout simple Spring Boot + Lombok example.
1. Maven dependencies for Lombok’s @Slf4j example
There are several logging frameworks implements Slf4j, we have used Logback in this example. Logback is intended as a successor to the popular log4j project which implements Slf4j natively. We have used logback-classic
maven dependency, it will pull the logback-core
and slf4j-api
dependencies.
Make sure you already installed Lombok setup for your IDE. To Setup in Eclipse or in Spring Tool Suite refer to our Lombok Maven example setup with Eclipse.
<dependencies> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> </dependencies>
2. A simple Lombok @Slf4j annotation example
2.1. Lombok’s @Slf4j generates a logger instance with default name log
using the SLF4J API. Note that we must also include a library that implements the Slf4j API.
@Slf4j public class LombokSlf4jDemo1 { public Double getSurcharge(Double transaction) { log.info("Surcharge Caliculation begins"); log.debug("Surcharge amount " + transaction); if (transaction.isNaN(transaction)) { log.error(transaction + " is not a valid amount"); throw new RuntimeException("Invalid Transaction"); } return Math.PI; } }
2.2. Lombok generated / DeLomoked code for LombokSlf4jDemo1.java
public class LombokSlf4jDemo1 { private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LombokSlf4jDemo1.class); public Double getSurcharge(Double transaction) { log.info("Surcharge Caliculation begins"); log.debug("Surcharge amount " + transaction); if (transaction.isNaN(transaction)) { log.error(transaction + " is not a valid amount"); throw new RuntimeException("Invalid Transaction"); } return Math.PI; } }
2.3. Testing LombokSlf4jDemo1.java :
public class LombokSlf4JDemo1Test { public static void main(String[] args) { new LombokSlf4jDemo1() .getSurcharge(0/0.0); } }
2.4. Output Results :
20:15:33.631 [main] INFO c.j.lombok.log.LombokSlf4jDemo1 - Surcharge Caliculation begins 20:15:33.674 [main] DEBUG c.j.lombok.log.LombokSlf4jDemo1 - Surcharge amount NaN 20:15:33.677 [main] ERROR c.j.lombok.log.LombokSlf4jDemo1 - NaN is not a valid amount Exception in thread "main" java.lang.RuntimeException: Invalid Transaction at com.javabydeveloper.lombok.log.LombokSlf4jDemo1.getSurcharge(LombokSlf4jDemo1.java:15) at com.javabydeveloper.demo.log.LombokSlf4JDemo1Test.main(LombokSlf4JDemo1Test.java:10)
2. Changing Lombok Logger field name using configuration
By default Lombok generates static
Logger instance with name log
. If you wants to change Logger filed name, following configuration in lombok.config
file allows you to to do so. Now we have to get Logger by using name logger
.
lombok.config :
# default value is "log" lombok.log.fieldName = logger # default value is "true" lombok.log.fieldIsStatic = false
DeLomoked code for LombokSlf4jDemo1.java after config change :
public class LombokSlf4jDemo1 { private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(LombokSlf4jDemo1.class); public Double getSurcharge(Double transaction) { log.info("Surcharge Caliculation begins"); log.debug("Surcharge amount " + transaction); if (transaction.isNaN(transaction)) { log.error(transaction + " is not a valid amount"); throw new RuntimeException("Invalid Transaction"); } return Math.PI; } }
3. Custom Logger using Lombok @CustomLog
To use custom logger we need to use @CustomLog annotation and following configuration in lombok.config
file. lombok.log.custom.declaration = LoggerType LoggerFactoryType.factoryMethod(TYPE|NAME|TOPIC|NULL)(TYPE|NAME|TOPIC|NULL)
Example : I am using java.util.logging.Logger
with following config for the logging to run a quick test. lombok.log.custom.declaration = java.util.logging.Logger java.util.logging.Logger.getLogger(NAME)(TOPIC)
lombok.config :
# default value is "log" lombok.log.fieldName = logger # default value is "true" lombok.log.fieldIsStatic = true lombok.log.custom.declaration = java.util.logging.Logger java.util.logging.Logger.getLogger(NAME)(TOPIC)
Lomboked LombokSlf4jDemo2.java :
@CustomLog public class LombokSlf4jDemo2 { public Double getSurcharge(Double transaction) { logger.info("Surcharge Caliculation begins"); logger.info("Surcharge amount " + "transaction"); if (transaction.isNaN(transaction)) { logger.warning(transaction + " is not a valid amount"); throw new RuntimeException("Invalid Transaction"); } return Math.PI; } }
DeLomboked LombokSlf4jDemo2.java :
public class LombokSlf4jDemo2 { private static final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(LombokSlf4jDemo2.class.getName()); public Double getSurcharge(Double transaction) { logger.info("Surcharge Caliculation begins"); logger.info("Surcharge amount " + "transaction"); if (transaction.isNaN(transaction)) { logger.warning(transaction + " is not a valid amount"); throw new RuntimeException("Invalid Transaction"); } return Math.PI; } }
4. Change Lombok’s log topic
By default generated Logger takes the type of class, to change that we need to set topic
attribute value with desired name.
@Slf4j(topic = "PaymentSurcharge") public class LombokSlf4jDemo1 { public Double getSurcharge(Double transaction) { logger.info("Surcharge Calculation begins"); logger.debug("Surcharge amount " + transaction); if (transaction.isNaN(transaction)) { logger.debug(transaction + " is not a valid amount"); throw new RuntimeException("Invalid Transaction"); } return Math.PI; } }
DeLomboked Code :
public class LombokSlf4jDemo3 { private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger("PaymentSurcharge"); public Double getSurcharge(Double transaction) { logger.info("Surcharge Calculation begins"); logger.debug("Surcharge amount " + transaction); return Math.PI; } }
Output Results :
12:05:58.412 [main] INFO PaymentSurcharge - Surcharge Calculation begins 12:05:58.439 [main] DEBUG PaymentSurcharge - Surcharge amount NaN 12:05:58.440 [main] DEBUG PaymentSurcharge - NaN is not a valid amount Exception in thread "main" java.lang.RuntimeException: Invalid Transaction at com.javabydeveloper.lombok.log.LombokSlf4jDemo1.getSurcharge(LombokSlf4jDemo1.java:16) at com.javabydeveloper.demo.log.LombokSlf4JDemo1Test.main(LombokSlf4JDemo1Test.java:10)
5. Conclusion
In this guide we have investigated how to work with Lombok @Slf4j annotation with several example for logging in application and also we have covered how to work with Lombok’s @CustomLog
for the custom logging. You can refer Delombok Maven example to see how looks like lombok generated code for your Lomboked classes.
You can checkout source code from our github repository.
You might be interested in our other following Lombok Tutorials :
- Lombok @Data
- Lombok @Builder
- Lombok @Builder.Default
- Lombok @Singular
- Lombok @NonNull
- Lombok Spring Boot example
Thanks worked, but log error is not showing in RED