Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot logging with Lombok

I would like to use Project Lombok's log annotation in my Spring Boot projects but I don't want to lose the functionality of being able to change the logging from the application.properties.

The Spring logging docs aren't overly clear on what the default logging implementation should be used, and there are 7 Lombok choices!

Any ideas?

like image 213
syncdk Avatar asked May 10 '17 20:05

syncdk


People also ask

What does Lombok use for logging?

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.

Is Slf4j part of Lombok?

Annotation Type Slf4jCauses lombok to generate a logger field. Complete documentation is found at the project lombok features page for lombok log annotations. This annotation is valid for classes and enumerations.

Is Lombok Slf4j vulnerable?

Lombok is a build-time only dependency; there is no need for lombok. jar to be available when your application is run, it just needs to be there when you compile your code. Therefore, lombok is highly unlikely to be a source of security vulnerabilities.


1 Answers

I would use @Slf4j. Tested the following and it works as expected.

@SpringBootApplication @Slf4j public class DemoApplication {      public static void main(String[] args) {         SpringApplication.run(DemoApplication.class, args);         log.info("testing logging with lombok");     } } 

Then you can change the logging level as described here.

logging.level.com.example.DemoApplication=WARN 

Note: Below clarifies that SLF4J is correctly handled but point is made in last 5 words!

From the docs:

Default configurations are provided for Java Util Logging, Log4J2 and Logback." ... "By default, If you use the ‘Starters’, Logback will be used for logging. Appropriate Logback routing is also included to ensure that dependent libraries that use Java Util Logging, Commons Logging, Log4J or SLF4J will all work correctly.

like image 159
Pär Nilsson Avatar answered Sep 21 '22 15:09

Pär Nilsson