Log4J Logging Methods
Logger class has a variety of methods to handle logging activities. The Logger class does not allow us to instantiate a new Logger instance, but it supports two static methods for obtaining a Logger object –
The first of the two methods return the root logger of application instance and it does not have a name.
Any other named Logger object instance is obtained by the second method by passing the name of the logger. The name of the logger can be any string that you pass, usually a class or a package name and it is mentioned below –
Example:
Logging Methods
Once we obtain an instance of a named logger, we can use numerous methods of the logger to log messages. The Logger class provides the following methods for printing the logging information.
debug: it is used to print the messages with the level Level.DEBUG.
Syntax:
error: It is used to print the messages with the level Level.ERROR.
Syntax:
fatal: It is used to print the messages with the level Level.FATAL.
Syntax:
info: It is used to print the messages with level Level.INFO.
Syntax:
warn: It is used to print the messages with level Level.WARN.
Syntax:
trace: It is used to print the messages with level Level.TRACE.
Syntax:
All the levels are defined in a class that class is org.apache.log4j.level class and any of the above mentioned methods can be called as follows:
The class name of the above class is tutoraspire. When you compile and run this program, it would generate the following output:
Debug Message! Info Message! Warn Message! Error Message! Fatal Message!
All the debug messages become more useful when they are used in combination with levels. We will cover levels in the next chapter and then, you would have a better understanding of how to use these methods in combination with different levels of debugging.