9.6 Example: separate class for logging 示例:用于记录的单独类

The second example involved error logging in a student project. A class contained several code sequences like the following:

第二个示例涉及学生项目中的错误记录。一个类包含几个代码序列,如下所示:

  1. try {
  2. rpcConn = connectionPool.getConnection(dest);
  3. } catch (IOException e) {
  4. NetworkErrorLogger.logRpcOpenError(req, dest, e);
  5. return null;
  6. }

Rather than logging the error at the point where it was detected, a separate method in a special error logging class was invoked. The error logging class was defined at the end of the same source file:

而不是在检测到错误时记录错误,而是调用特殊错误记录类中的单独方法。错误记录类是在同一源文件的末尾定义的:

  1. private static class NetworkErrorLogger {
  2. /**
  3. * Output information relevant to an error that occurs when trying
  4. * to open a connection to send an RPC.
  5. *
  6. * @param req
  7. * The RPC request that would have been sent through the connection
  8. * @param dest
  9. * The destination of the RPC
  10. * @param e
  11. * The caught error
  12. */
  13. public static void logRpcOpenError(RpcRequest req, AddrPortTuple dest, Exception e) {
  14. logger.log(Level.WARNING, "Cannot send message: " + req + ". \n" + "Unable to find or open connection to " + dest + " :" + e);
  15. }
  16. ...
  17. }

The NetworkErrorLogger class contained several methods such as logRpcSendError and logRpcReceiveError, each of which logged a different kind of error.

NetworkErrorLogger 类包含几个方法,例如 logRpcSendError 和 logRpcReceiveError,每个方法都记录了不同类型的错误。

This separation added complexity with no benefit. The logging methods were shallow: most consisted of a single line of code, but they required a considerable amount of documentation. Each method was only invoked in a single place. The logging methods were highly dependent on their invocations: someone reading the invocation would most likely flip over to the logging method to make sure that the right information was being logged; similarly, someone reading the logging method would probably flip over to the invocation site to understand the purpose of the method.

这种分离增加了复杂性,没有任何好处。日志记录方法很浅:大多数只包含一行代码,但是它们需要大量的文档。每个方法仅在单个位置调用。日志记录方法高度依赖于它们的调用:读取调用的人很可能会切换到日志记录方法,以确保记录了正确的信息。同样,阅读日志记录方法的人可能会转到调用站点以了解该方法的目的。

In this example, it would be better to eliminate the logging methods and place the logging statements at the locations where the errors were detected. This would make the code easier to read and eliminate the interfaces required for the logging methods.

在此示例中,最好消除日志记录方法,并将日志记录语句放置在检测到错误的位置。这将使代码更易于阅读,并消除了日志记录方法所需的接口。