5

On IntelliJ, I typically enable the inspection to find hard coded strings in Java projects. However, it can generate lot of false positives due to hard coded strings present in logger statements.

Is there any way in IntelliJ to ignore logger statements from this inspection ?

codehammer
  • 876
  • 2
  • 10
  • 27

1 Answers1

4

You can annotate the String parameter of the logger method with the @NonNls annotation. The Hard coded strings inspection will then ignore the strings present in logger calls. To annotate, position the text cursor on a warning and press Alt+Enter and select Annotate parameter 'xxx' as @NonNls.

Bas Leijdekkers
  • 23,709
  • 4
  • 70
  • 68
  • I would think this as a workaround though, basically polluting the code with such extraneous annotation on every logger statement. I guess there is no official support on this from IntelliJ, so would be good to add this to their feature list. They should just have a regex based support for ignoring on the actual statements as well. – codehammer Dec 10 '15 at 23:13
  • 1
    No, not on the logger statement, on the logger method. You add it from the call, but the annotation ends up on the method. Only one annotation is needed and **all** calls to the method are ignored. – Bas Leijdekkers Dec 11 '15 at 09:24
  • Hmm..that might work. will try that out for now. Although, I would still prefer something like this is supported natively on IDE so the code is free from such extraneous details (for good reason :)). So definitely would vote for this as a feature enhancement on future IntelliJ versions. – codehammer Dec 13 '15 at 08:35
  • 1
    You can store the annotations outside of the source code, using [External annotations](https://www.jetbrains.com/idea/help/external-annotations.html). – Bas Leijdekkers Dec 13 '15 at 08:51
  • The annotation on the logger declaration works as well. So for instance the following works, @NonNls private static final Logger LOGGER = LogManager.getLogger(MyClass.class); – Max Apr 26 '18 at 14:39