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.
Nobody here uses
LOG
orlog
? 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 alog
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.)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
andfinal
for loggers.It is not recommended to use multiple loggers (poor logging practice) rather than logging levels.
I think that
is the better option.
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.