Raje
Raje

Reputation: 3333

Which is the best way to declare logger variable in java

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.

Upvotes: 14

Views: 20290

Answers (12)

Ramji
Ramji

Reputation: 138

For ease of use, I prefer using slf4j and would like to use as below.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

Upvotes: 0

Pratik Patil
Pratik Patil

Reputation: 99

@Slf4j annotation can help with this.

import lombok.extern.slf4j.Slf4j;
@Slf4j
public class CustomClassName {
 void customMethod(){
  log.info("Shorthand logger annotation example");
 }
}

Upvotes: 0

EnGoPy
EnGoPy

Reputation: 353

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.

Upvotes: 0

ddekany
ddekany

Reputation: 31122

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.)

Upvotes: 0

yegor256
yegor256

Reputation: 105053

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

Upvotes: 0

Stan Kurilin
Stan Kurilin

Reputation: 15792

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.

Upvotes: 6

iKing
iKing

Reputation: 697

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.

Upvotes: 0

Shivan Dragon
Shivan Dragon

Reputation: 15219

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

Upvotes: 3

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.

Upvotes: 0

Sanjay T. Sharma
Sanjay T. Sharma

Reputation: 23208

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);

Upvotes: 14

Jan Zyka
Jan Zyka

Reputation: 17898

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

Upvotes: 1

Abhishek bhutra
Abhishek bhutra

Reputation: 1422

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

Upvotes: 0

Related Questions