14

I just wantd to know which is the best way declare logger variable in java. Following are some declaration.

1> private static final Logger logger = Logger.getLogger(ServiceImpl.class);

2> private static Logger logger = Logger.getLogger(ServiceImpl.class);

3> private static final Logger LOGGER= Logger.getLogger(ServiceImpl.class);

4> private static Logger LOGGER= Logger.getLogger(ServiceImpl.class);

P.S I really appreciate if anybody knows another best alternative ways to declare looger variable.

Raje
  • 3,285
  • 15
  • 50
  • 70
  • 2
    Up to you, but you should go for the `final` versions – fge Jan 06 '12 at 10:43
  • possible duplicate of [Should a "static final Logger" be declared in UPPER-CASE?](http://stackoverflow.com/questions/1417190/should-a-static-final-logger-be-declared-in-upper-case) – dogbane Jan 06 '12 at 10:43

11 Answers11

14

All upper-case variable names are IMO out because you really aren't declaring/defining a constant but a static variable. Uppercase names are more suitable for "constants". That said, I'd personally go with the first approach.

private static final Logger logger = Logger.getLogger(ServiceImpl.class);
Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71
  • 3
    I completely agree with your statement. Plus when you have LOGGER, it seems like that variable is just screaming out at you for your attention which should not happen. – Jeach Mar 13 '14 at 16:29
6

I vote for 3

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

It's final since you don't change it and it's in uppercase since it's a constant.

Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
  • 1
    It shoud NOT be uppercase. "Java Coding Style Guide", "Google Java Style" say, that it should be lower case "logger". It is beeing discussed here: http://stackoverflow.com/q/1417190/603314 – bugs_ Feb 08 '17 at 15:03
3

I personally think private static final Logger LOGGER= Logger.getLogger(ServiceImpl.class); is the best way to go for semantic and performance reasons:

  • your Log belongs to the class not to various instances of it, for this reason you should make it static
  • it should be private because it's used internally by the class, it's not part of it's public API
  • making it final two makes sense, first of because this states that the refference will not change (which is the case here) and second because final intance variables (especially static ones) can be better optimised for speed by the compiler and the JIT (more details here)
  • naming it all upper-case is really just a nice convention, that's how static variables are declared in Java, it's not really a must but it makes the code more readable
Shivan Dragon
  • 15,004
  • 9
  • 62
  • 103
1

I would go with the first option but it is matter of personal choice I guess.

Jan Zyka
  • 17,460
  • 16
  • 70
  • 118
  • but PMD rules says static final variable should be capital – Raje Jan 06 '12 at 10:48
  • 1
    @Raje: PMD is being intentionally obtuse in this case. Look at any static final field in a Java standard library class (E.g. `System`) which is not a "real" constant (i.e. not primitive or `String`). They don't use uppercase names for these variables. – Sanjay T. Sharma Jan 06 '12 at 11:02
0

I would recommend not to use such a variable in every class, but instead delegate this work to a static utility wrapper around slf4j, from jcabi-log:

Logger.debug(this, "some variable = %s", value);

Check this post as well: http://www.yegor256.com/2014/05/23/avoid-java-static-logger.html

yegor256
  • 102,010
  • 123
  • 446
  • 597
  • Though that's a bit tricky to implement if you want to have the caller's file name and line in your log statements, as opposed to the position from your utility class. But it's actually doable by utilizing `org.slf4j.spi.LocationAwareLogger`. – ddekany Oct 02 '17 at 21:49
0

It is good programming practice to share a single logger object between all of the instances of a particular class and to use the same logger for the duration of the program, so we use static and final for loggers.

It is not recommended to use multiple loggers (poor logging practice) rather than logging levels.

I think that

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

is the better option.

Leigh
  • 12,038
  • 4
  • 28
  • 36
iKing
  • 667
  • 10
  • 15
  • All of the logger declarations in the question are already `static` and declared as one variable, the question is asking whether it should be `final` and named in `UPPER` or `lower`. – Leigh May 21 '14 at 12:12
0

Nobody here uses LOG or log? I have found it to be nicer in practice. (Surely I'm not the first one to work on a place where that's the standard, because @Log4j in Lombok generates a log field as well. Of course it's also static final. And BTW, that's the best way to declare that field... add a @Log4j annotation. Done.)

ddekany
  • 29,656
  • 4
  • 57
  • 64
0

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

Where logger is a final, settled variable should be defined in UPPER CASE. While getLogger() static method need String as an argument, name of class should also invoke .getName() of class.

EnGoPy
  • 333
  • 4
  • 14
0

@Slf4j annotation can help with this.

import lombok.extern.slf4j.Slf4j;
@Slf4j
public class CustomClassName {
 void customMethod(){
  log.info("Shorthand logger annotation example");
 }
}
Pratik Patil
  • 99
  • 1
  • 7
0

First one is best.I prefer logger to be final and static....

Abhishek bhutra
  • 1,400
  • 1
  • 11
  • 29
  • @abhutra : thanks for quick reply . but PMD rules says static final variable should be capital. – Raje Jan 06 '12 at 10:47
0

In my understanding of the java style guide, 'logger' is best. 'LOGGER' would be for a defined constant.

Furthermore, using 'final', should make it a bit faster.

Thus #1.

James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78