java – 日志记录不适用于较低级别
作者:互联网
我正在使用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
谢谢,希望我已经提出了我的问题.
解决方法:
I am using java.util.logging, which works fine for higher levels above FINE but doesn’t work for lower levels, though I have set level as FINEST.
处理程序具有级别,但Logger还具有默认为INFO的级别.这是Logger构造函数中的行:
levelValue = Level.INFO.intValue();
即使处理程序中的级别低于INFO,如果记录器级别更高,那么这些日志消息都不会进入处理程序.在每种方法中都有以下检查:
if (Level.FINE.intValue() < levelValue) {
return;
}
如果您在主要内容中调用类似以下内容的内容,则将打印所有级别消息.
tracer.setLevel(Level.ALL);
标签:java-util-logging,java,logging 来源: https://codeday.me/bug/20190902/1792209.html