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.
The process of creating a new Logger in Java is quite simple. You have to use Logger. getLogger() method. The getLogger() method identifies the name of the Logger and takes string as a parameter.
Loggers should be declared to be static and final. 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.
A Logger object is used to log messages for a specific system or application component. Loggers are normally named, using a hierarchical dot-separated namespace. Logger names can be arbitrary strings, but they should normally be based on the package name or class name of the logged component, such as java.net or javax.
getLogger(java. lang. String): This method is used to find or create a logger with the name passed as parameter.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With