java - Tomcat 返回带有一字节响应的 HTTP 500

标签 java scala tomcat tomcat8 lift

我一直在尝试追踪我的应用程序发送的一些 HTTP 500。它们在 localhost_access_log.txt 中显示为:

127.0.0.1 - - [30/Aug/2017:16:27:46 +0000] "POST /user/174 HTTP/1.1" 500 1

所以我在我的应用程序中添加了日志记录,以捕获所有类型为 5xx 的异常和响应。这些日志永远不会被填充。另外,如您所见,响应的长度是一个字节,这绝不应该是这种情况。我使用的网络框架 Lift 将返回比 5xx 更大的响应。

我正试图捕获所有问题:

LiftRules.onEndServicing.append {
    case (req, Full(resp)) => {
      resp.toResponse.code.toString match {
        case internalServerError if(internalServerError.startsWith("5")) => requestLogError(req, resp.toString)
        case _ => 
      }
    }
    case _ => 
}

LiftRules.exceptionHandler.prepend {
  case (runMode, req, exception) =>
    requestLogError(req, Throwables.getStackTraceAsString(exception))
    XhtmlResponse((<html> <body>Something unexpected happened while serving the page at {req.uri}</body> </html>), S.htmlProperties.docType, List("Content-Type" -> "text/html; charset=utf-8"), Nil, 500, S.legacyIeCompatibilityMode)
}

因为 requestLog 中没有记录任何内容,所以我想知道它是否会影响我的代码。

如何在 Tomcat 中启用更好的日志记录以查看根本原因?

catalina.out 中也没有任何内容。

最佳答案

5xx 类型的错误表示服务器出现意外错误。理想情况下,开发人员应该将所有可能的 try-catch block 放在代码中,这样就不会发生这些 5xx 错误。同样, try catch 所有类型的错误,而不是在代码中寻找特定的 5xx 错误,否则这些日志将永远不会被记录。

关于java - Tomcat 返回带有一字节响应的 HTTP 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45965593/

相关文章:

java - 实现 URL 过滤器

java - 如何从html :options collections获取选定选项值

java - 如何限制方法只接受对象作为参数而不是类对象作为类型文字?

java - 用 Maven 重命名一个 fat jar

scala - 字符串插值和宏 : how to get the StringContext and expression locations

drupal - 自定义 java webapp 和 drupal 之间的 SSO 集成

java - 标准 API JPA

java - 在没有数组变量的情况下迭代java中的静态数组

scala - 对同一张 table 使用光滑的多重投影

javascript警告框在运行tomcat时无法在Eclipse中弹出