Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux编程 > » 正文

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

来源: Poechant 分享至:

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.



Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史