java - 正确使用log4j和异常

标签 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);

提前致谢。

最佳答案

异常只是信号/事件;在您的应用程序范围内发生的情况。记录这些是另一个主题。

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

最简单的情况可能是:

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 还是只想向调用者发出发生异常的信号?或者两者兼而有之(就像上面的例子一样?)。

在使用异常或任何其他方式传播意外状态之前,请回答以下问题:应如何处理这种情况?异常后应该发生什么?日志记录也不异常(exception);这只是记录。找到答案后,您可以寻找适当的方式来传达意外状态。

关于java - 正确使用log4j和异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30568673/

相关文章:

java - 将文件保存到 Jersey API 中的服务器应用程序目录

使用 Mockito 进行 Java HttpClient 单元测试

java - 检查用户输入是否为 Int,无异常?

java - Hibernate 异常没有被捕获

java - 如何在 spring 的 xmls 中定义变量以在 log4j.properties 中使用

Scala sbt 控制台(启动菊石外壳): How to Disable debug logging

java - Apache 兴趣点: "Cannot resolve symbol"

java - Play 编译错误无法访问值 <root>.api 中的术语库。当前类路径可能缺少 <root>.api.libs 的定义,

javascript/jquery 错误 "invalid object initializer"

java - 是否可以使用默认初始化过程在 EJB 模块中初始化 log4j?