
本文介绍如何针对非 200 HTTP 响应,在独立的日志文件中记录更详细的信息,包括请求方法、路径和响应状态码。我们将探讨使用 Logback 配置实现这一目标,确保只在发生错误时才记录这些额外信息,从而避免污染常规日志。
在 Web 应用开发中,记录 HTTP 响应状态码以及相关信息对于问题排查和性能监控至关重要。特别是对于非 200 响应(例如 404 Not Found, 500 Internal Server Error),我们需要记录请求的详细信息,以便快速定位问题。本文将介绍如何使用 Logback 配置,在独立的日志文件中记录这些非 200 响应的详细信息,包括请求方法、路径和响应状态码。
使用 Logback 实现非 200 响应的独立日志记录
Logback 是一个强大的 Java 日志框架,它提供了灵活的配置选项,可以满足各种日志需求。我们可以通过配置 Logback 来实现只在发生非 200 响应时,才将请求方法、路径和响应状态码记录到独立的日志文件中。
实现思路如下:
定义一个 Logger: 创建一个专门用于记录非 200 响应的 Logger。配置 Appender: 为该 Logger 配置一个 Appender,将日志输出到独立的日志文件。使用 Filter 过滤日志: 配置一个 Filter,只允许非 200 响应的日志通过。
示例 Logback 配置 (logback-spring.xml)
logs/non-200-responses.log true %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %level %logger{36} - %msg%n message.contains("response: 200") == false ACCEPT DENY
配置详解:
MacsMind
电商AI超级智能客服
141 查看详情
: 定义了一个名为 Non200ResponseAppender 的 Appender,用于将日志输出到 logs/non-200-responses.log 文件。: 使用 EvaluatorFilter 来过滤日志。message.contains(“response: 200”) == false: 使用表达式判断日志消息是否包含 “response: 200″,如果不包含,则允许通过。: 定义了一个名为 non200ResponseLogger 的 Logger,并将 Non200ResponseAppender 配置给它。additivity=”false” 阻止日志被 root logger 重复处理。
代码示例 (Controller)
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class ProductController { private static final Logger logger = LoggerFactory.getLogger(ProductController.class); private static final Logger non200ResponseLogger = LoggerFactory.getLogger("non200ResponseLogger"); @GetMapping("/product/{id}") public ResponseEntity getProduct(@PathVariable("id") Long id) { logger.info("Fetching product with id: {}", id); if (id > 100) { String message = String.format("method: GET, path: /product/%d, response: 404 NOT_FOUND", id); non200ResponseLogger.warn(message); return new ResponseEntity("Product not found", HttpStatus.NOT_FOUND); } else { String message = String.format("method: GET, path: /product/%d, response: 200 OK", id); non200ResponseLogger.warn(message); // 这条信息不会被记录到 non-200-responses.log return new ResponseEntity("Product found", HttpStatus.OK); } }}
代码详解:
首先,我们获取了两个 Logger 实例,一个是用于记录常规日志的 logger,另一个是用于记录非 200 响应的 non200ResponseLogger。注意 non200ResponseLogger 的名称需要和 logback-spring.xml 中定义的 logger 的 name 属性一致。在 getProduct 方法中,我们首先使用 logger.info 记录常规日志。如果 id 大于 100,我们使用 non200ResponseLogger.warn 记录一条包含请求方法、路径和响应状态码的日志。这条日志将被记录到 logs/non-200-responses.log 文件中。如果 id 小于等于 100,我们也使用 non200ResponseLogger.warn 记录一条包含请求方法、路径和响应状态码的日志,但是因为状态码为 200,这条日志不会被记录到 logs/non-200-responses.log 文件中。
注意事项:
确保 Logback 的配置文件 (logback-spring.xml) 位于 classpath 下。需要根据实际情况调整 Appender 的配置,例如日志文件的路径、日志格式等。EvaluatorFilter 使用 Groovy 表达式,需要确保项目中包含 Groovy 依赖。
总结:
通过配置 Logback,我们可以轻松地实现非 200 HTTP 响应的独立日志记录。这种方法可以帮助我们快速定位问题,提高开发和运维效率。 通过灵活的配置,我们可以根据实际需求,记录更详细的信息,例如请求头、请求体等。 此外,还可以将日志发送到远程服务器,以便进行集中管理和分析。
以上就是如何记录非 200 HTTP 响应的详细信息的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/773196.html
微信扫一扫
支付宝扫一扫