Logger 日志处理主类,是Api入口
LoggerManager 日志管理器,Logger的工厂类,进行初始化Logger,Handler,Formatter等
Handler
将日志信息记录到指定的流之中,可能是控制台或者是一个文件或者是任意存储区域。
常用实现类 ConsoleHandler, FileHandler, SocketHandler,一个Logger可以有多个handler
Formatter 日志信息格式化类。常用实现类 SimpleFormatter, XMLFormatter,可以自定义类,每个handler都可以持有一个formatter,默认ConsoleHandler使用SimpleFormatter,而FileHandler使用XMLFormatter
Level
日志记录级别。
SEVERE(最高值) 、WARNING 、INFO 、CONFIG 、FINE 、FINER 、FINEST(最低值)
ALL(记录所有信息) OFF(不记录任何级别信息)
Filter
过滤器,不同与日志级别,用于确定是否处理日志,,每个handler都可以持有一个filter,Logger会全局持有一个filter
LogRecord
日志内容的封装类
MyLogManager.java:
package com.hx.log.jdk.custom;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.*;
public class MyLogManager {
public static void resetFromPropertyFile(String filePath) {
LogManager logManager = LogManager.getLogManager();
logManager.reset();
InputStream inputStream = MyLogManager.class.getClassLoader().getResourceAsStream(filePath);
try {
logManager.readConfiguration(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static Logger getLogger(String name) {
Logger logger = Logger.getLogger(name);
logger.setLevel(Level.ALL);
for (Handler handler : logger.getHandlers()) {
logger.removeHandler(handler);
}
// add console handler
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.INFO);
consoleHandler.setFormatter(new MySimpleFormatter());
consoleHandler.setFilter(new Filter() {
@Override
public boolean isLoggable(LogRecord record) {
return true;
}
});
logger.addHandler(consoleHandler);
// add file handler
FileHandler fileHandler;
try {
fileHandler = new FileHandler("file.log", true);
fileHandler.setLevel(Level.INFO);
fileHandler.setFormatter(new MySimpleFormatter());
logger.addHandler(fileHandler);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// add default stream handler
MyStreamHandler defaultStreamHandler = new MyStreamHandler();
defaultStreamHandler.setLevel(Level.INFO);
defaultStreamHandler.setFormatter(new MySimpleFormatter());
defaultStreamHandler.setOutputStream(System.out);
logger.addHandler(defaultStreamHandler);
return logger;
}
}
MySimpleFormatter.java:
package com.hx.log.jdk.custom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;
public class MySimpleFormatter extends SimpleFormatter {
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public synchronized String format(LogRecord record) {
StringBuilder sb = new StringBuilder();
Level level = record.getLevel();
String time = sdf.format(new Date(record.getMillis()));
String loggerName = record.getLoggerName();
String message = record.getMessage();
sb.append("[").append(level).append("]");
sb.append(" ").append(time);
sb.append(" ").append(loggerName);
sb.append(" :").append(message);
sb.append("
");
return sb.toString();
}
}
MyStreamHandler.java:
package com.hx.log.jdk.custom;
import java.io.OutputStream;
import java.util.logging.StreamHandler;
public class MyStreamHandler extends StreamHandler {
@Override
public synchronized void setOutputStream(OutputStream out)
throws SecurityException {
super.setOutputStream(out);
}
}
Test.java:
package com.hx.log.jdk.custom;
/**
* Created by hx on 17-1-7.
*/
import junit.framework.TestCase;
import java.io.IOException;
import java.util.logging.Filter;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
public class Test extends TestCase{
public void testDefault(){
Logger logger=Logger.getLogger(Test.class.getName());//使用默认的配置
logger.setFilter(new Filter() {
@Override
public boolean isLoggable(LogRecord record) {
return true;
}
});
logger.info(" test!");
}
public void testMy() throws SecurityException, IOException {
MyLogManager.resetFromPropertyFile("log/jdk/logging.properties");
Logger logger=MyLogManager.getLogger(Test.class.getName());
logger.setFilter(new Filter() {
@Override
public boolean isLoggable(LogRecord record) {
return true;
}
});
logger.info(" test!");
}
}
使用方法testdefault,只是在控制台打印信息:ConsoleHandler(System.err)
使用方法testMy测试,会输出到五个目的地,包括自定义配置文件的ConsoleHandler(System.err),FileHandler(u.xml文件)和
MyLoggerManager中添加的ConsoleHandler(System.err),FileHandler(file.log)和MyStreamHandler(System.out)