如何在Wiremock中配置跳过健康检查类特定请求的日志记录
Scala 2.13 + WireMock 2.28.1 跳过健康检查路径日志的解决方案
方案1:直接修改Stub映射配置(推荐,侵入性最低)
你当前使用的2.28.1版本原生支持在Stub规则中添加disableLogging参数,可直接关闭对应规则匹配到的请求的所有日志输出,无需修改业务代码。调整后的映射文件如下:
{ "mappings": [ { "request": { "method": "GET", "url": "/api/v1/health/ready" }, "response": { "status": 200, "body": "", "headers": { "Content-Type": "text/plain" } }, "disableLogging": true }, { "request": { "method": "GET", "url": "/api/v1/health/alive" }, "response": { "status": 200, "body": "", "headers": { "Content-Type": "text/plain" } }, "disableLogging": true } ] }
方案2:自定义Notifier全局过滤(适合批量匹配多类路径)
如果存在大量需要过滤的路径,不想逐个修改Stub,可以自定义带过滤规则的Notifier替换默认的ConsoleNotifier,实现灵活的全局日志过滤:
第一步:实现自定义Notifier
import com.github.tomakehurst.wiremock.common.ConsoleNotifier class HealthCheckFilteredNotifier(verbose: Boolean) extends ConsoleNotifier(verbose) { // 匹配所有需要跳过日志的健康检查路径 private val skipPathRegex = "^/api/v1/health/.*".r override def info(message: String): Unit = { // 过滤掉包含健康检查路径的日志 if (!skipPathRegex.pattern.matcher(message).find()) { super.info(message) } } // 可按需重写error、requestReceived等方法实现更细粒度的日志控制 }
第二步:修改WireMock初始化配置
把原有配置中的ConsoleNotifier替换为自定义的Notifier即可:
new WireMockConfiguration() .bindAddress(configOptions.host) .port(configOptions.port) .stubCorsEnabled(configOptions.enableCors) .disableRequestJournal() // avoid JVM heap exhaustion for recorded requests .usingFilesUnderClasspath(s"$confBasePath/${configOptions.mappingsFolder}") .notifier(new HealthCheckFilteredNotifier(configOptions.verboseNotifier)) // 替换为自定义实现 .httpsPort(...)
内容的提问来源于stack exchange,提问作者sentenza
相关产品推荐
相关产品推荐

