jdk自带的Logger使用简介

3 篇文章 0 订阅
日志工具类【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");
}

}



引自:[url]http://www.oschina.net/code/snippet_133198_22810[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值