35

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?

syncdk
  • 2,820
  • 3
  • 25
  • 31

1 Answers1

65

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.

2240
  • 1,547
  • 2
  • 12
  • 30
Pär Nilsson
  • 2,259
  • 15
  • 19
  • 1
    Great, with this setup can you change the logging levels from the `application.properties` file? – syncdk May 10 '17 at 20:21
  • 2
    So my application was not a springboot application but I was using spring framework component like JDBCTemplate for connecting to database and lombok slf4J for logging which has default logging level to DEBUG . I ended up using logback.xml file under resources/ because as per spring documentation mentioned above , it uses logback.xml for logging when used with slf4j. – Shrikant Prabhu Nov 08 '18 at 18:28
  • My logback file looked like this : ` %d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n ` – Shrikant Prabhu Nov 08 '18 at 18:30
  • 1
    The last paragraph that mentions log4j2 / logback in the first sentence.. that threw me off this answer, and I lost a day of fruitless hunting as a result. Maybe helpful to others to stress the the last five words ("SLF4J will all work correctly"). Heck, I'll try to edit to do just that.. – Razzle Mar 13 '20 at 10:15