簡體   English   中英

Java Logger產生多個相同的日志到控制台

[英]Java Logger Producing Multiple of the Same Log to Console

我有以下代碼,我們沒有使用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

您將在每次對'printColumnStats'的調用上添加一個新的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。 您的記錄器應將UserParentHandlers設置為false,這樣您也不會發布到該處理程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM