log4j one of the FAQ

Source: Internet
Author: User
Tags log4j

Log4j This dongdong, everyone has been using, but really how many people know, why to be like that with the column. Or how many do not copy can own a log4j column. Readers can ask themselves if they can do it. In this chapter, we will analyze some important links combined with source code.

First, log4j is the place to read the configuration file.

Before answering this question, first think about how we use log4j to print the log in the program.

1. Obtain logger, such as

private static Logger Logger = Logger.getlogger (App.class);
2. Call the Logger method in the method print log

Logger.info ("This is the Main method");

So we followed the GetLogger () method to see the implementation and found that the method was set as follows:

  Logger GetLogger (Class clazz) {return
    Logmanager.getlogger (Clazz.getname ());
He is entrusted to Logmanager to achieve. So we followed up Logmanager, by looking at the structure of the class, found that there is a static initial block, we read the code, found that the original log4j configuration file is read here, And he is first to find log4j.xml, if not found to find Log4j.properteis.

In fact, there is a solution to this problem is to log4j the source code, the form of the project into the Eclipse/idea, and then the global search log4j.properties or Log4j.xml. Why do you want to search these 2 file names. Because the log4j default configuration file name is these 2, then he in the program, there will certainly be a constant to define the 2 file name, even if no one constant, there will be directly based on these 2 file name loading configuration. This is the normal logic of a program developer, and the processing method.

Second, we are in the log4j.properties, often will be configured with a log4j.rootlogger. What is its role.

We have the rootlogger,appender,layout in the configuration file, why log in our program will output columns according to these specifications. Does our own definition of logger have anything to do with this rootlogger?

In fact from naming, we are not guessing to Rootlogger, is the superclass column of other logger we define. Indeed, indeed. We all the way steadfast GetLogger () method, has been tracked to the Hierarchy.getlogger ();

Public Logger GetLogger (String name, Loggerfactory Factory) {//system.out.println ("getinstance") +name+. ")
    Categorykey key = new Categorykey (name); Synchronize to prevent write conflicts.
    Read conflicts (In//Getchainedlevel method) are possible only if variable//assignments are.

    Logger Logger;
      Synchronized (HT) {Object o = ht.get (key);
	if (o = = null) {logger = factory.makenewloggerinstance (name);
	Logger.sethierarchy (this);
	Ht.put (key, logger);
	Updateparents (logger);
      return logger;
      else if (o instanceof Logger) {return (Logger) O;
	else if (o instanceof provisionnode) {//system.out.println ("+name+") Ht.get (This) returned Provisionnode ");
	Logger = factory.makenewloggerinstance (name);
	Logger.sethierarchy (this);
	Ht.put (key, logger);
	Updatechildren ((Provisionnode) O, logger);
	Updateparents (logger);
      return logger; else {//It should be impossible to arrive this return null; But let ' s keep the compiler happy. }

In fact, the logger we create will be stored in a Hashtable named HT. The key is updateparents (logger); This method, we further trace this method, found that if a looger can not find the parent logger, then
  if (!parentfound)
      cat.parent = root;
will have its parent set to Rootlogger. The specific code of this method, I will not post, we can go to see for themselves. So Rootlogger thing (appender,layout), is not he can directly inherit come over to use the column.

So about Rootlogger's inheritance system and dependencies, I drew a simpler UML sketch

(~~~~~~~~~~~~~~~~~~~ picture occupies a position, csdn upload picture and hang out ~~~~~~~~~~~~~~~~~~~~~~~)

Upload pictures tried n times are hung off, can only describe it, or we go in to look at the class structure

Rootlogger inherits logger, and logger inherits to category.

Category inside there is a variable Appenderattachableimpl aai; This class is packaged with a protected Vector appenderlist to store the Appender.

It can be seen that the relationship between logger and Appender is 1:n.

As for the Appender inheritance system too compare me, do not describe, everyone to see themselves. Said so much, everyone back to look after the configuration file, is not suddenly understand a lot.

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.