Java日志服务入门系列教程——(2)Apache log4j入门

1. 概述

目前log4j有三个发展分支,一个是1.2的稳定版,一个是已经停止继续发展的1.3版本,另一个就是实验阶段的2.0版本。Apache log4j官网上称,在一台载有运行在800MHz的AMD Duron CPU并使用JDK 1.3.1 上的计算机中,log4j 1.2 只需要5纳秒就可以完成一个日志语句是应该作为日志输出还是不输出的决定。


2. 下载log4j 1.2 API library

从人人网的实验室下载:

http://labs.renren.com/apache-mirror//logging/log4j/1.2.16/apache-log4j-1.2.16.zip

从BJTU下载:

http://mirror.bjtu.edu.cn/apache//logging/log4j/1.2.16/apache-log4j-1.2.16.zip


3. log4j的Hello World 程序源代码

package com.sinosuperman.log4j;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class Test {
	private static final Logger logger = Logger.getLogger(Test.class);
	public static void main(String[] args) {
		BasicConfigurator.configure();
		logger.info("Hello World, This is an information message.");
		logger.error("Hello World, This is a error message.");
		logger.warn("Hello World, This is a warning message.");
		logger.debug("Hello World, This is a debugging message.");
		logger.fatal("Hello World, This is a fatal message.");
		System.exit(0);
	}
}

4. log4j的Hello World 程序运行结果

1 [main] INFO com.sinosuperman.log4j.Test  - Hello World, This is an information message.
3 [main] ERROR com.sinosuperman.log4j.Test  - Hello World, This is a error message.
3 [main] WARN com.sinosuperman.log4j.Test  - Hello World, This is a warning message.
3 [main] DEBUG com.sinosuperman.log4j.Test  - Hello World, This is a debugging message.
3 [main] FATAL com.sinosuperman.log4j.Test  - Hello World, This is a fatal message.


你可能感兴趣的:(java,apache,log4j,String,library,debugging)