簡體   English   中英

使用 log4j 和 Solarlint 兼容記錄異常的正確方法

[英]Correct way to log exceptions using log4j and Solarlint compliant

      public static Date convertStringToDate(String cellText) throws 
      ParseException {
        Date date = null;
        SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yy");
        try {
          date = dateFormatter.parse(cellText);
        } catch (ParseException e) {
           logger.error("Error in parsing string to date" + e);
        }
        return date;
      }

我正在使用 log4j 框架在我的項目中記錄異常。我已經開始使用 Solarlint 來評估我的 static 代碼。 但是對於 logger.error 消息,它說我需要使用格式說明符而不是字符串連接。 有人可以告訴我如何在符合 Solarlint 的 fashian 中使用 logger.error("Error in parsing String to Date" + e) 。 其他帖子中提到的例子很少,但我不太明白這是我再次問的原因。

因此,您的聲納正在哭泣,因為您將error messageException e連接起來

logger.error("Error in parsing string to date" + e);

相反,您可以將異常作為參數添加到logger.error方法。 像這樣的東西:

logger.error("Error in parsing a string to date: {}", e.getMessage(), e);

或者

logger.error("Error in parsing string to date", e);

這將解決格式說明符而不是字符串連接的問題以及聲納問題。

暫無
暫無

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

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