正确使用log4j和Exceptions

时间:2015-06-01 08:16:19

标签: java exception log4j

我对使用异常的log4j表示怀疑。 我想将消息记录到我的日志文件中,我不知道如何处理异常。 我必须只使用异常(因为已经在我的日志文件中打印)或其他一些东西:

try {
        Configurations.getInstance().getProperty("DynamoDBProfileCredentials");
        credentials = new ProfileCredentialsProvider( Configurations.getInstance().getProperty("DynamoDBProfileCredentials")).getCredentials();

    } catch (Exception e) {
        log.error(new AmazonClientException("Cannot load the credentials from the credential profiles file. " +
                "Please make sure that your credentials file is at the correct " +
                "location (C:\\Users\\your username\\credentials), and is in valid format.",e));

或者

try {
        Configurations.getInstance().getProperty("DynamoDBProfileCredentials");
        credentials = new ProfileCredentialsProvider( Configurations.getInstance().getProperty("DynamoDBProfileCredentials")).getCredentials();

    } catch (Exception e) {
        log.error("Cannot load the credentials from the credential profiles file. " +
                "Please make sure that your credentials file is at the correct " +
                "location (C:\\Users\\your username\\credentials), and is in valid format.",e);
        throw new AmazonClientException(
                "Cannot load the credentials from the credential profiles file. " +
                        "Please make sure that your credentials file is at the correct " +
                        "location (C:\\Users\\your username\\credentials), and is in valid format.",
                        e);

提前致谢。

1 个答案:

答案 0 :(得分:1)

例外只是信号/事件;发生在您的应用程序范围内。记录这些是一个不同的主题。

我理解您需要在应用程序中记录有用的消息。在您的情况下,您可以在方法的使用者上或直接触发日志事件。

最简单的情况可能是:

try {
  Configurations.getInstance().getProperty("DynamoDBProfileCredentials");
  credentials = new ProfileCredentialsProvider( Configurations.getInstance().getProperty("DynamoDBProfileCredentials")).getCredentials(); 
} catch (Exception e) {
  AmazonClientException ace = AmazonClientException(
      "Cannot load the credentials from the credential profiles file. " +
      "Please make sure that your credentials file is at the correct " +
      "location (C:\\Users\\your username\\credentials), and is in valid format.", e);

  log.error(ace.getMessage(), ace);
  throw ace;
}

try {
  Configurations.getInstance().getProperty("DynamoDBProfileCredentials");
  credentials = new ProfileCredentialsProvider( Configurations.getInstance().getProperty("DynamoDBProfileCredentials")).getCredentials(); 
} catch (Exception e) {
  log.error("Cannot load the credentials from the credential profiles file. " +
      "Please make sure that your credentials file is at the correct " +
      "location (C:\\Users\\your username\\credentials), and is in valid format.",e);
  throw new AmazonClientException(e);
}

上面的示例只是一个实现,但不是原始问题的答案。要回答原始问题,您必须清楚了解您的API以及API设计所产生的责任。你想在你的API中登录,或者你想给呼叫者一个信号,是否发生了异常?或两者(就像上面的例子中那样?)。

在使用例外或任何其他方式传播意外状态之前,请回答问题:如何处理此案例?异常后会发生什么?记录也不例外;它只是记录。找到答案后,您可以寻找合适的方式来传达意外状态。