我有以下代码,我们没有使用System.out.println
语句,但必须使用记录器打印到控制台。
以下是示例代码(Java):
public void printColumnStats() {
java.util.logging.Logger log = java.util.logging.Logger
.getLogger("ProfileStatusClass");
log.setLevel(Level.ALL);
ConsoleHandler handler = new ConsoleHandler();
handler.setFormatter(new MyFormatter());
handler.setLevel(Level.ALL);
log.addHandler(handler);
// This will print the current Column Profiling stats
log.fine("FieldName : " + this.datasetFieldName);
log.fine("Field index : " + this.fieldIndex);
NumberFormat formatter = new DecimalFormat("#0.00000000");
if (this.fieldType.equalsIgnoreCase("number")) {
log.fine("Field Null Count : " + this.datasetFieldNullCount);
log.fine("Field Valid/Obs Count : " + this.datasetFieldObsCount);
log.fine("Field Min : " + (0l + this.datasetFieldMin));
...
我有以下要求(对不起这部分是在Scala中,但应该是直截了当的:
for (e <- tResults) {
e._2.printColumnStats()
println("++........................................................++")
}
即使每种类型的循环只有一种,我在下一组统计数据拉升之前会得到大量的重复:
Field Null Count : 0.0
Field Null Count : 0.0
Field Null Count : 0.0
Field Null Count : 0.0
Field Null Count : 0.0
Field Null Count : 0.0
Field Null Count : 0.0
Field Null Count : 0.0
Field Null Count : 0.0
Field Null Count : 0.0
Field Null Count : 0.0
Field Null Count : 0.0
Field Null Count : 0.0
Field Null Count : 0.0
Field Null Count : 0.0
答案 0 :(得分:1)
您每次调用&#39; printColumnStats&#39;时都会添加一个新的ConsoleHandler。您只想安装一个处理程序。如果您要使用代码设置记录器,则将设置代码移出printColumnStats函数并进入静态块。
private static final Logger log = Logger.getLogger("ProfileStatusClass");
static {
log.setLevel(Level.ALL);
ConsoleHandler handler = new ConsoleHandler();
handler.setFormatter(new MyFormatter());
handler.setLevel(Level.ALL);
log.addHandler(handler);
}
默认情况下,JVM也会在根记录器上安装ConsoleHandler。您的记录器应将setUserParentHandlers设置为false,这样您也不会发布到该处理程序。