Java lombok почему не видит log
Перейти к содержимому

Java lombok почему не видит log

  • автор:

Building with Lombok’s @Slf4j and Intellij: Cannot find symbol log

I have a maven project that builds with no problems from the command line. However, when I build it with IntelliJ, I get the error:

There is no log defined or imported in the java file, but there is a

statement before the class body which should define the log class.

In the project structure window, classes for:

are listed under libraries and are indicated as having been downloaded and available.

Any idea why this would build with maven through the command line, but not through IntelliJ and how to resolve the issue?

30 Answers

In addition to having Lombok plugin installed, also make sure that the » Enable annotation processing » checkbox is ticked under:

Note: starting with IntelliJ 2017, the » Enable Annotation Processing » checkbox has moved to:

Presumably, that’s the Lombok @Slf4j annotation you’re using. You’ll need to install the Lombok plugin in IntelliJ if you want IntelliJ to recognize Lombok annotations. Otherwise, what do you expect if you try to use a field that doesn’t exist?

In Intellij version 2016, 2017, enable Preferences -> Compiler -> Annotation Processors does not work for me!

The following additional checkbox helps: enter image description here

2019:

Get a plugin and you are sorted.

File > Settings > Plugins

enter image description here

There is the following step to be followed here:

Step 1. Enabled annotation processing for your project under File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processor

Screenshot enter image description here

Step 2. Install lombok plugin in IntelliJ IDE after that restart IDE. Screenshot

enter image description here

Step 3. Add the dependency in build.gradle file.

In case you are using lombok in tests you need to add:

hope this answer is helpful for you.

Worked for me. It was failing on CircleCI & on Jenkins as well.

If you’re a Gradle User try add the following into your dependencies:

I might be ungraving a dead topic but a simple solution is to check in your dependencies (Maven’s pom for exemple) if you are including logback-core and logback-classic.

Slf4j is just the interface, you need the concrete implementation behind it to work.

I’ve been tricked twice with IDEA messing it up, now I’m good to go 😀

So if the issue persists even after enabling the annotation processing and installing the Lombok plugin.

There’s an issue with IDEA 2020.3 and Lombok, you could fix this by following this fix.

basically, add -Djps.track.ap.dependencies=false to the VM options, you can find it in: Preferences -> Compiler. Named ‘Shared build process VM Options’

Itsn’t a IntelliJ problem. If you try under console, run mvn install, also breaks. All annotations from lombok.extern needed add dependencies. This package groups the next annotations:

  • CommonsLog
  • Flogger
  • Log
  • JBossLog
  • Log4
  • Log4j2
  • Slf4j
  • XSlf4j

For example, for Slf4j it’s necessary add this dependency to your pom.xml

If you are using maven, try adding Lombok path to maven-compiler-plugin list of annotation processor as shown below.

Change the version as per your version of Lombok. Other than that ensure you have done the following

  • installed the Lombok plugin for Intellij.
  • Enabled annotation processing for your project under File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processor . For me both, Obtain processors from project classpath and Processor path is working. So not sure what will work for you, but try whichever works.

And rather than shooting in the dark for hours. Reading a little bit how annotation processors work and are used by compiler may help. so have quick read below.

In IDEA 13 this seems to no longer be an issue, you just have to have the Lombok plugin installed.

I’ve just installed the latest idea verion 2108.1 and found this issue, after installed lombok plugin and restart the Idea resolve it.

I tried almost all of the mentioned answers but nothing worked for me. My gradle build was failing every time. Just found this solution:

Add annotationProcessor ‘org.projectlombok:lombok’ in your build.gradle.

This worked for me.

Removing the @Slf4J annotation from the class and then re-adding it worked for me.

1 My gradle lombok dependecies:

2 After enabling «Annotations. » in IDEA (Settings), taking into account that you have installed Lombok plugin, that resolved my the same issue

This worked for me : File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processor

Tick on ‘enable annotation processing’. Apply

I had the same kind of error, right after a warning like the following:

I was seeing this issue with an older version of Lombok when compiling under JDK8. Setting the project back to JDK7 made the issue go away.

This won’t have been OP’s problem, but for anyone else who tries everything with no success:

I had similar symptoms. Whenever I built after a mvn clean , it wouldn’t find log , or getXYZ() , or builder() , or anything.

After reading every answer I could find about QueryDSL/JPA/Hibernate/Lombok/IntelliJ/Maven issues to no avail, I worked out that the culprit was a single static import of a @Getter method that was annotated on a static field.

Building with Lombok's @Slf4j and Intellij: Cannot find symbol log

In addition to having Lombok plugin installed, also make sure that the » Enable annotation processing » checkbox is ticked under:

Note: starting with IntelliJ 2017, the » Enable Annotation Processing » checkbox has moved to:

Solution 2

Presumably, that’s the Lombok @Slf4j annotation you’re using. You’ll need to install the Lombok plugin in IntelliJ if you want IntelliJ to recognize Lombok annotations. Otherwise, what do you expect if you try to use a field that doesn’t exist?

Solution 3

There is the following step to be followed here:

Step 1. Enabled annotation processing for your project under File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processor

Screenshot enter image description here

Step 2. Install lombok plugin in IntelliJ IDE after that restart IDE. Screenshot

enter image description here

Step 3. Add the dependency in build.gradle file.

In case you are using lombok in tests you need to add:

hope this answer is helpful for you.

Solution 4

In Intellij version 2016, 2017, enable Preferences -> Compiler -> Annotation Processors does not work for me!

The following additional checkbox helps: enter image description here

@Slf4j LOG variable is not recognised by Intellij IDEA 15 #154

Not a show stopper rather annoying. A fix would be appreciated.

Thank you for your help.

The text was updated successfully, but these errors were encountered:

Hi, thanks. I did that still has no luck.

Intellij is still complaining about LOG variable. Other lombok annotations
are working fine.

Kind Regards
Senthil.

On Mon, Nov 23, 2015 at 7:09 PM, Tomek notifications@github.com wrote:

Hi, I am using intellij 15 with @slf4j https://github.com/Slf4j and for
me everything works fine. In my case help me File > Invalidate Caches /
Restart > Invalidate and Restart


Reply to this email directly or view it on GitHub
#154 (comment)
.

Double check annotation processing and lombok enabled in project settings.

By default, Lombok generates a variable called log (lower-case). If you placed a new variable name in lombok.config, you have to make sure it’s in the src/main/java . Lombok itself will search higher up in te structure, however IDEA plugin currently has a limitation where it searches only up to default package.

Could it be that you are hitting that specific issue?
Reason why I’m asking is that I’m on IDEA 15 and latest plugin version and am using LOG variable, which is set in my lombok.config via lombok.log.fieldName = LOG . But I did have to move the file to the default package, until more flexible placement is supported by the plugin.

After update to 0.9.7.15 with IDEA 15.0.2. I’ve experienced almost the same issues with with @slf4j annotations when I do ‘make project’ by pressing hot key Command(Ctrl) + F9, IDEA cause error:
Error:(79, 9) java: cannot find symbol
symbol: variable log
location: class
And point on the line, where I have:
log.error(«Error . «);

*Update: Checked with Idea 14.1.6, 15.0.2 / 15.0.3 / 16 EAP 0.9.8.14 (0.9.8.15)
The result is the same.
Checked in another module within the same project and I don’t have such issue. Lombok dependency is the same like in version I have an issue. Will try to reproduce that thing in another clear project soon and will try to add sources, if I’ll be succeed in that.

After update to 0.9.8.15 (IDEA 15.0.3) I have the same problem now. previously it was working (don’t know if I had 0.9.7 or 0.9.6). Now also other annotations seems to fail, but I cannot see a pattern there

Interesting. Im using @Slfj4 and a bunch of other annotations and the latest plugin works fine. However, contrary to you, I’m on EAP 16 version.

Looks like I’ve found solution: close IDEA project, delete .idea folder and .iml files in each module, reimport project. Now I can Make project w.o. any issue (at least not with @Slf4j annotation).
Another solutions like change IDEA versions (checked on 14.1.6, 15.0.2, 15.0.3, 16 EAP), change lombok plugin version, upgrade lombok dependency itself, even upgrade JDK doesn’t make any success results for me.
Hope this solution could be useful.

I will close this issue now, because it is not reproducible for me. I hope it is something with your local setup.
Feel free to open new issue if problem still exists!

Building with Lombok's @Slf4j and Intellij: Cannot find symbol log

I have a maven project that builds with no problems from the command line. However, when I build it with IntelliJ, I get the error:

There is no log defined or imported in the java file, but there is a

statement before the class body which should define the log class.

In the project structure window, classes for:

are listed under libraries and are indicated as having been downloaded and available.

Any idea why this would build with maven through the command line, but not through IntelliJ and how to resolve the issue?

37 Answers 37

In addition to having Lombok plugin installed, also make sure that the » Enable annotation processing » checkbox is ticked under:

Note: starting with IntelliJ 2017, the » Enable Annotation Processing » checkbox has moved to:

Alexander Zagniotov's user avatar

Presumably, that’s the Lombok @Slf4j annotation you’re using. You’ll need to install the Lombok plugin in IntelliJ if you want IntelliJ to recognize Lombok annotations. Otherwise, what do you expect if you try to use a field that doesn’t exist?

Ryan Stewart's user avatar

There is the following step to be followed here:

Step 1. Enabled annotation processing for your project under File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processor

Screenshot enter image description here

Step 2. Install lombok plugin in IntelliJ IDE after that restart IDE. Screenshot

enter image description here

Step 3. Add the dependency in build.gradle file.

In case you are using lombok in tests you need to add:

hope this answer is helpful for you.

In Intellij version 2016, 2017, enable Preferences -> Compiler -> Annotation Processors does not work for me!

The following additional checkbox helps: enter image description here

2019:

Get a plugin and you are sorted.

File > Settings > Plugins

enter image description here

Worked for me. It was failing on CircleCI & on Jenkins as well.

If you’re a Gradle User try add the following into your dependencies:

S34N's user avatar

I might be ungraving a dead topic but a simple solution is to check in your dependencies (Maven’s pom for exemple) if you are including logback-core and logback-classic.

Slf4j is just the interface, you need the concrete implementation behind it to work.

I’ve been tricked twice with IDEA messing it up, now I’m good to go 😀

So if the issue persists even after enabling the annotation processing and installing the Lombok plugin.

There’s an issue with IDEA 2020.3 and Lombok, you could fix this by following this fix.

basically, add -Djps.track.ap.dependencies=false to the VM options, you can find it in: Preferences -> Compiler. Named ‘Shared build process VM Options’

If you are using maven, try adding Lombok path to maven-compiler-plugin list of annotation processor as shown below.

Change the version as per your version of Lombok. Other than that ensure you have done the following

  • installed the Lombok plugin for Intellij.
  • Enabled annotation processing for your project under File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processor . For me both, Obtain processors from project classpath and Processor path is working. So not sure what will work for you, but try whichever works.

And rather than shooting in the dark for hours. Reading a little bit how annotation processors work and are used by compiler may help. so have quick read below.

Meena Chaudhary's user avatar

Itsn’t a IntelliJ problem. If you try under console, run mvn install, also breaks. All annotations from lombok.extern needed add dependencies. This package groups the next annotations:

  • CommonsLog
  • Flogger
  • Log
  • JBossLog
  • Log4
  • Log4j2
  • Slf4j
  • XSlf4j

For example, for Slf4j it’s necessary add this dependency to your pom.xml

albertoiNET's user avatar

I tried almost all of the mentioned answers but nothing worked for me. My gradle build was failing every time. Just found this solution:

Add annotationProcessor ‘org.projectlombok:lombok’ in your build.gradle.

This worked for me.

Amrit Kr Lama's user avatar

In IDEA 13 this seems to no longer be an issue, you just have to have the Lombok plugin installed.

I’ve just installed the latest idea verion 2108.1 and found this issue, after installed lombok plugin and restart the Idea resolve it.

mefor sy's user avatar

Removing the @Slf4J annotation from the class and then re-adding it worked for me.

1 My gradle lombok dependecies:

2 After enabling «Annotations. » in IDEA (Settings), taking into account that you have installed Lombok plugin, that resolved my the same issue

None of the answers address the fact that you might have incorrect dependencies in your project, if you are using spring boot the dependency definition is easy:

But if you are not using spring boot you can import all dependencies:

This assuming you have the plugin working and the "Enable Annotation Processing" checkbox activated in:

Settings[Ctrl+Alt+S] > Build, Execution, Deployment > Compiler > Annotation Processors

This worked for me : File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processor

Tick on ‘enable annotation processing’. Apply

Sumukh Bhandarkar's user avatar

Try to create lombok.config file under project base directory and provide lombok.log.fieldName value.

Example: lombok.log.fieldName = LOG

Taras Melnyk's user avatar

I had the same kind of error, right after a warning like the following:

Cos64's user avatar

IDEA 2021 and still have the issue, to solve it make sure that you have the last version of lombok in your project dependencies.

I was seeing this issue with an older version of Lombok when compiling under JDK8. Setting the project back to JDK7 made the issue go away.

John Chapman's user avatar

This won’t have been OP’s problem, but for anyone else who tries everything with no success:

I had similar symptoms. Whenever I built after a mvn clean , it wouldn’t find log , or getXYZ() , or builder() , or anything.

After reading every answer I could find about QueryDSL/JPA/Hibernate/Lombok/IntelliJ/Maven issues to no avail, I worked out that the culprit was a single static import of a @Getter method that was annotated on a static field.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *