日志记录不适用于较低级别
我正在使用 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. I have tried giving it Level.ALL also. I have user defined class as follows.
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");
}
}
When I run main method, I get the console and file output as follows
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
Thanks, Hope I have put forward my question clear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
处理程序具有级别,但 Logger 也具有默认为 INFO 的级别。这是
Logger
构造函数中的一行:即使处理程序中的级别低于
INFO
,如果记录器级别较高,那么这些日志消息都不会到达处理程序。在每个fine
和其他方法中,都有如下检查:如果您在
main
中调用类似以下内容,则将打印所有级别消息。The handlers have levels but the
Logger
also has a level which defaults toINFO
. Here's the line from theLogger
constructor:Even if the level in the handlers is lower than
INFO
, if the logger level is higher then none of those log messages will make it to the handlers. In each of thefine
and other methods there are checks like:If you call something like the following in your
main
then all of the level messages will be printed.您需要在记录器中的处理程序以及记录器本身上设置日志级别。日志记录仅在两个级别中的“最粗略”级别执行。这是一个完成这项工作的日志记录类。
You need to set the log level on both the handlers in the logger, and the logger itself. Logging is only performed at the "coarsest" of the two levels. Here is a logging class that does the job.