Apache log4j Usages

Source: Internet
Author: User
Tags log4j

1.Logger class

The Rootlogger is logger.getrootlogger by the static method of the Logger class. All other loggers are instantiated and obtained by means of static method Logger.getlogger. This method logger.getlogger the desired logger name as an argument. Some other basic methods of the Logger class are listed below:

Package org.apache.log4j;

public class Logger {
  // Creation and retrieval methods:
  public static Logger getRootLogger();
  public static Logger getLogger(String name);
  // printing methods:
  public void debug(Object message);
  public void info(Object message);
  public void warn(Object message);
  public void error(Object message);
  public void fatal(Object message);
  // generic printing method:
  public void log(Level l, Object message);
}

2. GetLogger method

The GetLogger method is called with the same parameter name, and the returned reference always points to the exact same logger object. For example, here:

Logger x = Logger.getLogger("wombat");
Logger y = Logger.getLogger("wombat");

X and y point to the exact same logger object.

3.log4j Use Process

1 Initialize log4j according to configuration file

Log4j can be initialized with 3 configurator:

Basicconfigurator,domconfigurator,propertyconfigurator.

Here's the propertyconfigurator. The use of Propertyconfigurator applies to all systems. such as the following statement.

Propertyconfigurator.configure ("Log4j.properties");

The log4j environment is initialized with Log4j.properties for the configuration file. For General Java project to initialize log4j,log4j without using the above statement, the configuration file is automatically found and initialized under Classpath. If the log4j cannot initialize the configuration file automatically, it needs to be initialized with the above method.

Note: Initializing the configuration file is best done only once when the system is started, if executed multiple times, one is a waste of resources, the other is for the old version of the log4j, the use of dailyrollingfileappender, there may be problems.

2) Import Org.apache.log4j.Logger and related packages.

3 Get the log instance where you need to use log4j.

private static Logger log = Logger.getlogger ("Myclass.class");

4) using the debug,info,fatal of the Logger object ... Method.

Log.debug ("It is the Debug info");

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.