views:

1136

answers:

9

In Java, static final variables are constants and the convention is that they should be in upper-case. However, I have seen that most people declare loggers in lower-case which comes up as a violation in PMD.

e.g:

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

Just search googleor SO for "static final logger" and you will see this for yourself.

Should we be using LOGGER instead?

+1  A: 

I personally think it looks really big in upper-case. Moreover, since it's a class that it's not directly related to the class behaviour, I don't see a major problem in using logger instead of LOGGER. But if you are going to be strictly pedantic, then use LOGGER.

JG
+1  A: 

According to Sun's Code Conventions, constants should be in uppercase with words separated with an underscore. (I still think it looks pretty horrible ^^)

So, yes: LOGGER.

Bryan Menard
+2  A: 

Not necessarily. Read this - Naming Conventions

SUMMARY: The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_").

adatapost
+4  A: 

If you are using an automated tool to check your coding standards and it violates said standards then it or the standards should be fixed. If you're using an external standard, fix the code.

The convention in Sun Java is uppercase for public static constants. Obviously a logger is not constant, but represents a mutable thing ( otherwise there would be no point calling methods on it in the hope that something will happen ); there's no specific standard for non-constant final fields.

Pete Kirkham
Why are you saying the logger is not constant? It seems constant indeed. The logging is produces is a side-effect of calling its methods, but don't change its observable state. Did I miss something?
KLE
Check the API. It does have an add/get pair of methods. But you reasoning is flawed anyway. Logging is observable (otherwise, what's the point).
Tom Hawtin - tackline
If it were a StringBuilder rather than a logger, then it would perhaps be more obviously non-constant. Even for loggers, methods such as Logger.setLevel() do mutate the receiver observably. Generally uppercase is for those constants which the languages treats as constants and will inline.
Pete Kirkham
The logger is not a constant as it is a reference to an object. Constants are values that can't be changed. The object reference is final (so the reference to it can't be changed, e.g. swapped with something else or set to null) but the object itself can.
Spoike
+9  A: 

The logger reference is not a constant, but a final reference, and should NOT be in uppercase. A constant VALUE should be in uppercase.

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

private static final double MY_CONSTANT = 0.0;
crunchdog
+1  A: 

If you google this, you might find that in some cases, the loggers are not defined as static final. Add some quick copy-n-paste to this, and this might explain it.

We use LOGGER in all our code, and this corresponds to our naming convention (and our CheckStyle is happy with it).


We even go further, taking advantage of the strict naming convention in Eclipse. We create a new class with a code template of :

    // private static final Logger LOGGER = Logger.getLogger(${enclosing_type}.class);

${current_class} should be replaced with the correct syntax for Eclipse, I forgot it.

The logger is commented out, as initially we don't need it. But should we need it later, we just uncomment it.

Then in the code, we use code templates that expect this logger to be present. Example with the try-catch template:

    try {
      ${cursor} or some other template
    } catch (Exception t) {
      LOGGER.error("${methodName} ${method parameters}", t);
    }

We have a few more templates that use it.

The strict convention allow us to be more productive and coherent with code templates.

KLE
Catching Throwable is bad practice, unless you log and rethrow it. Remember Errors: OutOfMemeoryError, etc.Event Exception is not so safe to be catched and handled by yourself in multi-thread applications.
Vitaly Polonetsky
Correct. I change the code to reflect your insight.
KLE
Eclipse syntax is: Logger.getLogger(${enclosing_type}.class);
dogbane
@fahdshariff Thanks for the precise syntax. I updated my answer.
KLE
+1  A: 

Usually constants are in uppercase.

Loggers, however, should not be static but looked up for every "new" of the containing class if using the slf4j facade. This avoids some nasty classloader issues in notably web containers, plus it allows the logger framework to do special stuff depending on the invocation context.

Thorbjørn Ravn Andersen
+1  A: 

If your coding standards - if you have any - say that it should be uppercase then yes.

I don't see any stringent reason for one way or the other. I think it totally depends on your personal likes resp. your company coding standards.

BTW: I prefer "LOGGER" ;-)

Kutzi
+1  A: 

Don't forget that PMD will respect a comment with

// NOPMD

in it. This will cause PMD to skip the line from its checks, this will allow you to choose whichever style you want.

Fortyrunner
Or dont use PMD, they are always wrong and your code is perfect
01