Reputation: 3333
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
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
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
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
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
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
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
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
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:
Upvotes: 3
Reputation: 12624
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
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
Reputation: 17898
I would go with the first option but it is matter of personal choice I guess.
Upvotes: 1
Reputation: 1422
First one is best.I prefer logger to be final
and static
....
Upvotes: 0