12

Using the Eclipse Checkstyle plugin I see this error:

Name 'logger' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.

I resolved this error by changing:

private static final Logger logger = Logger.getLogger(someClass.class);

to

private static final Logger LOGGER = Logger.getLogger(someClass.class);

Why is this a checkstyle warning?

javaPlease42
  • 4,699
  • 7
  • 36
  • 65

2 Answers2

18

Because the field is marked final and static which implies that it's a constant and should be named with uppercase letters.

From this link, you can see that the module ConstantName has the format ^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$ which is exactly the one your Checkstyle plugin has specified.

M A
  • 71,713
  • 13
  • 134
  • 174
  • But `LOGGER` should be `logger` by Java naming convention so this is kind of a CheckStyle bug? see this [post](http://stackoverflow.com/questions/1417190/should-a-static-final-logger-be-declared-in-upper-case). – javaPlease42 Jan 27 '15 at 19:40
  • 1
    @javaPlease42 It depends on how you define a constant. I don't think `logger` is a constant, so yes the rule doesn't really make sense here. – M A Jan 27 '15 at 20:05
  • If it is static final then it should be considered as a constant and it should be uppercase by Java naming convention. – user2189998 Jan 25 '16 at 15:19
5

The documentation recommends using this configuration if you wish to keep logger as a valid option:

<module name="ConstantName">
  <property name="format"
    value="^log(ger)?$|^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/>
</module>
    
lightswitch05
  • 9,058
  • 7
  • 52
  • 75