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.
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.
First one is best.I prefer logger to be
final
andstatic
....I personally think
private static final Logger LOGGER= Logger.getLogger(ServiceImpl.class);
is the best way to go for semantic and performance reasons:I would go with the first option but it is matter of personal choice I guess.
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:
Check this post as well: http://www.yegor256.com/2014/05/23/avoid-java-static-logger.html
I vote for 3
It's
final
since you don't change it and it's in uppercase since it's a constant.