log4j的默认加载过程

在没有知道log4j配置文件的情况下,也能打印日志,这是因为log4j在调用Logger.getLogger时,就会自动去classpath下面找配置文件的。具体代码:

package com.coderdream.log4j;
import org.apache.log4j.Logger;
import org.apache.log4j.helpers.LogLog;
public class HelloLog4j {
    private static Logger logger;
    static{
        LogLog.setInternalDebugging(true);
        logger = Logger.getLogger(HelloLog4j.class);
    }	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
	    
		// System.out.println("This is println message.");
		// 记录debug级别的信�?
		logger.debug("This is debug message.");
		// 记录info级别的信�?
		logger.info("This is info message.");
		// 记录error级别的信�?
		logger.error("This is error message.");
	}
}


 

LogLog.setInternalDebugging(true);  打开这个开关,可以看到log4j内部的初始化日志。方便理解。


输出结果:

log4j: Trying to find [log4j.xml] using context classloader [email protected]: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader@126b249 class loader.log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().log4j: Trying to find [log4j.properties] using context classloader [email protected]: Using URL [file:/D:/projs/java-test/bin/log4j.properties] for automatic log4j configuration.log4j: Reading configuration from URL file:/D:/projs/java-test/bin/log4j.propertieslog4j: Parsing for [root] with value=[debug,appender1].log4j: Level token is [debug].log4j: Category root set to DEBUGlog4j: Parsing appender named "appender1".log4j: Parsing layout options for "appender1".log4j: End of parsing for "appender1".log4j: Parsed "appender1" options.log4j: Finished configuring.[main] DEBUG com.coderdream.log4j.HelloLog4j - This is debug message.[main] INFO com.coderdream.log4j.HelloLog4j - This is info message.[main] ERROR com.coderdream.log4j.HelloLog4j - This is error message. 

具体实现类:

___________         ______________

|LogManager|----->|OptionConverter |

 

 

参考:

log4j 简明手册

你可能感兴趣的:(log4j,ClassLoader,File,Class,token,Parsing)