Loggers in Java are objects which trigger log events, They are created and are called in the code of the application, where they generate Log Events before passing them to the next component which is an Appender. You can use multiple loggers in a single class to respond to various events or use Loggers in a hierarchy. They are normally named using the hierarchical dot-separated namespace. Also, all the Logger names must be based on the class or the package name of the logged component.
Apart from this, each Logger keeps a track of the nearest existing ancestor in the Logger namespace and also has a “Level” associated with it.
Create new Logger:
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. So, if a Logger pre-exists then, that Logger is returned, else a new Logger is created.
Syntax:
Here, MyClass is the class name for which we are getting the Logger object.
static Logger logger = Logger.getLogger(MyClass.class.getName());
// Example:
public class Student{
private static final Logger LOGGER = Logger.getLogger(Student.class);
public void getStudentDetails() {
}
}