java 日志 logging

源码:看注释

package com.jdk14logger;

import java.io.IOException;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class jdklog {

private static String name = "";
private static Logger log = null;

public static Logger getLog(String names){
if(log==null){
log=Logger.getLogger(name);
String logPath = "C:/logger.log";//这个是你指定的log文件的路径
FileHandler fileHandler;
try {
fileHandler = new FileHandler(logPath);
fileHandler.setFormatter(new TextFormatter());//新建一个格式类,让它指定
log.setUseParentHandlers(false);
log.addHandler(fileHandler);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
return log;
}
final static class TextFormatter extends Formatter {

/**
* The constructor to use.
*/
public TextFormatter() {
}

/**
* Formats the given log record's data into human-readable text.
*
* @param record The log record whose data needs to be formatted.
* @return The log record's data as a string.
*/
public String format(LogRecord record) {
String level;
if (record.getLevel() == Level.INFO) {
level = "INFO";
} else if (record.getLevel() == Level.ALL) {
level = "ALL";
} else if (record.getLevel() == Level.SEVERE) {
level = "SEVERE";
} else if (record.getLevel() == Level.WARNING) {
level = "WARNING";
} else {
level = "UNKNOWN";
}

//you can build your own result format

String result ="";
result += "\n\t" + level + ": " + record.getMessage().replaceAll("\n", "\n\t");


return result;
}
}


}

调用:Logger log=jdklog.getLog(test.class.getName());
log.info(需要打印的东西);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值