The logger class provides several ways to handle log activity. The Logger class does not allow the instantiation of a new logger instance, but it provides two static methods to obtain a Logger object:
The first return application instance of the two methods here does not have a name for the root logger. Any other named Logger object instance is obtained through the name of the logger through the second method. A logger name is a name that can be passed on any string, usually a class or package, as we have used in the last chapter.
Static Logger log = Logger.getlogger (log4jexample. Class. GetName ());
Logging Method:
After we get an instance called a logger, we can use several methods of logging to record messages. The logger class has the following methods specifically for printing log information:
SN |
Method and Description |
1 |
public void Debug (Object message) This method prints using the LEVEL.DEBUG message level |
2 |
public void error (Object message) This method prints using the LEVEL.ERROR message level |
3 |
public void fatal (Object message); This method prints using the Level.fatal message level |
4 |
public void info (Object message); This method prints using the Level.info message level |
5 |
public void warn (Object message); This method prints using the Level.warn message level |
6 |
public void Trace (Object message); This method prints using the Level.trace message level |
All levels are defined in the Org.apache.log4j.Level class, and any of the above methods can be called as follows:
ImportOrg.apache.log4j.Logger; Public classLogclass {Private StaticOrg.apache.log4j.Logger log =Logger. GetLogger (Logclass.class); Public Static voidMain (string[] args) {Log.trace ("Trace message!"); Log.debug ("Debug message!"); Log.info ("Info message!"); Log.warn ("Warn message!"); Log.error ("Error message!"); Log.fatal ("Fatal message!"); }}
When you compile and run the Logclass program, the following results are produced:
Debug message! Info Message! Warn Message! Error Message! Fatal Message!
All debug messages make more sense when they are used in a level combination. Levels are described in the next chapter, there is a good understanding and how to use these methods to debug at different levels in the next section.
log4j Tutorial 6, Logger method