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.


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钟超

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值