源码:看注释
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(需要打印的东西);
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(需要打印的东西);