jdk自带的Logger使用简介

日志工具类【JavaLog.java】
/**
 * JavaLog.java
 * cn.com.songjy.test.log
 * Function: TODO 
 *
 *   version    date      author
 * ──────────────────────────────────
 *   	1.0	 2013-7-10    songjy
 *
 * Copyright (c) 2013, TNT All Rights Reserved.
*/

package cn.com.songjy.test.log;

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.LogManager;
import java.util.logging.Logger;

/**
 * ClassName:JavaLog
 *
 * @author   songjy
 * @version  1.0
 * @since    v1.0
 * @Date	 2013-7-10	上午9:29:23
 */

public class JavaLog {

	private static Logger log = null;
	
	private JavaLog(){}
	
	public static Logger getLogger(){
		InputStream is = null;
		try {
			if(null == log){
				//is = JavaLog.class.getResourceAsStream("/logger.properties");//也可
				is  = JavaLog.class.getClassLoader().getResourceAsStream("logger.properties");
				LogManager.getLogManager().readConfiguration(is);
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(null != is) is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		log = Logger.getLogger("LOGGER");
		
		return log;
	}
	
}


创建配置文件【logger.properties】
handlers = java.util.logging.ConsoleHandler,java.util.logging.FileHandler

java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.level = INFO

java.util.logging.FileHandler.pattern = target/my.log%g.log
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.limit = 104857600
java.util.logging.FileHandler.count = 3
java.util.logging.FileHandler.append = true
java.util.logging.FileHandler.level = INFO

LOGGER.level = FINEST

测试代码【TestLog.java】
/**
 * TestLog.java
 * cn.com.songjy.test.log
 * Function: TODO 
 *
 *   version    date      author
 * ──────────────────────────────────
 *   	1.0	 2013-7-10    songjy
 *
 * Copyright (c) 2013, TNT All Rights Reserved.
*/

package cn.com.songjy.test.log;

import java.util.logging.Logger;

/**
 * ClassName:TestLog
 *
 * @author   songjy
 * @version  1.0
 * @since    v1.0
 * @Date	 2013-7-10	上午9:58:39
 */

public class TestLog {

	public static void main(String[] args) {
		Logger log = JavaLog.getLogger();
		log.fine("fine");
		log.finer("finer");
		log.finest("finest");
		log.info("info");
		log.config("config");
		log.warning("warning");
		log.severe("severe");
	}

}



引自: http://www.oschina.net/code/snippet_133198_22810

你可能感兴趣的:(jdk,日志,Logger)