1

Can anybody help me with @Slf4j annotation in Lombok?

I prefer to log something in my SpringBoot app. For this I want to use @Slf4j annotation from Lombok. I installed Lombok plugin in my IntelliJ IDEA, turn on 'Annotation processing', add @Slf4j annotation to my class and add lombok dependency to my pom.xml file. Now I can find object 'log' in the class, but I can't apply any log methods to it (like error(), debug(), info(), etc.). Why aren't these methods found?

IntelliJ IDEA Community 2020.3
Lombok plugin bundled 203.7717.56
lombok 1.18.6

dependency in pom.xml:

<properties>
    <lombok.version>1.18.6</lombok.version>
</properties>

<dependencies>
     <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <version>${lombok.version}</version>
     </dependency>
</dependencies>

logging in my class:

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
@Service
public class ImportServiceImpl implements ImportService {
   ...
   public void importData() {
      log.info("some log");
   }
   ...
}
  • What methods are shown on the object? Are there any at all? And you also are sure, that you have the slf4j dependencies on the classpath? – dunni Mar 30 '21 at 18:58
  • @dunni There are no any methods. IDE offers to continue my 'log.' with 'args', 'field', 'cast', 'castvar' and other java construction short forms. No, I aren't sure (see screenshot - https://prnt.sc/1104hgm). I thought, if I add lombok dependency, then slf4j will be added to my project transitively (implicitly). If no, should I add extra dependency for slf4j? – Oleg Sandro Mar 30 '21 at 19:27
  • No, Lombok doesn't add any transitive dependencies. In your case, if you add spring-boot-starter-logging, you have all dependencies you need for logging. – dunni Mar 30 '21 at 19:28
  • @dunni Thanks a lot! You helped me! I had to add dependency spring-boot-starter-logging (or alternatively dependency logbook-spring-boot-starter), but it didn't work for me at once, also I had to click the 'Reload all Maven Projects' button in Maven tab of my IntelliJ IDEA, because for some reason I didn't get the effect instantly, when adding dependencies mentioned – Oleg Sandro Mar 30 '21 at 20:15
  • Regarding the IntelliJ issue, there is also the setting to enable Auto-Import, when you do any changes in your pom.xml. You can find it in Settings -> Build, Execution, Deployment -> Build Tool, and then enable the checkbox "Reload project after changes in the build script", and check "Any changes". – dunni Mar 30 '21 at 20:20
  • @dunni Thank you! Nice recommendation. I will use it – Oleg Sandro Mar 31 '21 at 05:43

2 Answers2

2

Lombok itself does not bring any dependencies regarding log frameworks etc. You need to add the log framework dependencies yourself.

E.g. in your case, add the following dependency, it will bring SLF4J as well as Logback as the logging implementation:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
</dependency>
dunni
  • 43,386
  • 10
  • 104
  • 99
  • I am having the same issue. I have added this dependency (with Gradle) but it doesn't seem to be working. – Pawan Apr 27 '21 at 18:11
0

I had everything in place. The build was working and I was able to run the application without any compilation error. Just that the IDE was complaining.

In my case I had to Invalidate Caches.

Go to File > Invalidate Caches (select "Clear file system caches and local history") > Invalidate and Restart.

Hope that works.

Pawan
  • 1,183
  • 16
  • 29