我正在使用 java.util.logging,它适用于高于 FINE 的更高级别,但不适用于较低级别,尽管我已将级别设置为 FINEST。我也试过给它 Level.ALL。我有如下用户定义的类。
public class Tracer extends Logger {
public Tracer(String name) {
super(name, null);
addFileHandler();
addConsoleHandler();
}
private void addConsoleHandler() {
SSFormatter ssFormatter = new SSFormatter();
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setFormatter(ssFormatter);
consoleHandler.setLevel(Level.FINEST);
this.addHandler(consoleHandler);
}
private void addFileHandler() {
try {
FileHandler fileHandler = new FileHandler("log.txt", false);
fileHandler.setFormatter(new SSFormatter());
fileHandler.setLevel(Level.FINEST);
this.addHandler(fileHandler);
} catch (SecurityException e) {
e.printStackTrace();
this.warning("SecurityException occurred while adding FileHandler");
} catch (IOException e) {
e.printStackTrace();
this.warning("IOException occurred while adding FileHandler");
}
}
private class SSFormatter extends Formatter {
@Override
public String format(LogRecord paramLogRecord) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(paramLogRecord.getLoggerName()).append("|")
.append(paramLogRecord.getSourceClassName()).append("|")
.append(paramLogRecord.getSourceMethodName()).append(":::")
.append(paramLogRecord.getMessage()).append("\n");
return stringBuilder.toString();
}
}
public static void main(String[] args) {
Tracer tracer = new Tracer("ss.tracer");
tracer.info("This is to test my tracer");
tracer.warning("This is warning from my tracer");
tracer.severe("This is severe from my tracer");
tracer.fine("this is fine");
tracer.finer("this is finer");
tracer.finest("this is finest");
}
}
当我运行 main 方法时,我得到控制台和文件输出如下
ss.tracer|screen.seizer.trace.Tracer|main:::This is to test my tracer
ss.tracer|screen.seizer.trace.Tracer|main:::This is warning from my tracer
ss.tracer|screen.seizer.trace.Tracer|main:::This is severe from my tracer
谢谢,希望我已经清楚地提出了我的问题。