log4j Tutorial 6, Logger method

Source: Internet
Author: User

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:

    • public static Logger Getrootlogger ();

    • public static Logger GetLogger (String name);

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.