From 2784f6008e97c7283b2e57e98d18a89fe8f530ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Thu, 4 Jan 2024 11:47:59 +0100 Subject: [PATCH] Only log status in ServletRequestHandledEvent This commit updates the description of RequestHandledEvent to avoid providing a misleading status as the absence of a failure logs OK which can be inaccurate. Closes gh-27595 --- .../context/support/RequestHandledEvent.java | 13 ++--- .../support/ServletRequestHandledEvent.java | 3 +- .../support/RequestHandledEventTests.java | 50 +++++++++++++++++++ .../ServletRequestHandledEventTests.java | 47 +++++++++++++++++ 4 files changed, 103 insertions(+), 10 deletions(-) create mode 100644 spring-web/src/test/java/org/springframework/web/context/support/RequestHandledEventTests.java create mode 100644 spring-web/src/test/java/org/springframework/web/context/support/ServletRequestHandledEventTests.java diff --git a/spring-web/src/main/java/org/springframework/web/context/support/RequestHandledEvent.java b/spring-web/src/main/java/org/springframework/web/context/support/RequestHandledEvent.java index 4ee3ec2367a0..bc60bb4308d2 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/RequestHandledEvent.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/RequestHandledEvent.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -146,15 +146,10 @@ public String getDescription() { StringBuilder sb = new StringBuilder(); sb.append("session=[").append(this.sessionId).append("]; "); sb.append("user=[").append(this.userName).append("]; "); - sb.append("time=[").append(this.processingTimeMillis).append("ms]; "); - sb.append("status=["); - if (!wasFailure()) { - sb.append("OK"); + sb.append("time=[").append(this.processingTimeMillis).append("ms]"); + if (wasFailure()) { + sb.append("; failure=[").append(this.failureCause).append("]"); } - else { - sb.append("failed: ").append(this.failureCause); - } - sb.append(']'); return sb.toString(); } diff --git a/spring-web/src/main/java/org/springframework/web/context/support/ServletRequestHandledEvent.java b/spring-web/src/main/java/org/springframework/web/context/support/ServletRequestHandledEvent.java index e864033b5113..c0a5bf117a3a 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/ServletRequestHandledEvent.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/ServletRequestHandledEvent.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -174,6 +174,7 @@ public String getDescription() { sb.append("url=[").append(getRequestUrl()).append("]; "); sb.append("client=[").append(getClientAddress()).append("]; "); sb.append("method=[").append(getMethod()).append("]; "); + sb.append("status=[").append(getStatusCode()).append("]; "); sb.append("servlet=[").append(getServletName()).append("]; "); sb.append(super.getDescription()); return sb.toString(); diff --git a/spring-web/src/test/java/org/springframework/web/context/support/RequestHandledEventTests.java b/spring-web/src/test/java/org/springframework/web/context/support/RequestHandledEventTests.java new file mode 100644 index 000000000000..aed73a411133 --- /dev/null +++ b/spring-web/src/test/java/org/springframework/web/context/support/RequestHandledEventTests.java @@ -0,0 +1,50 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.web.context.support; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link RequestHandledEvent}. + * + * @author Stephane Nicoll + */ +class RequestHandledEventTests { + + @Test + void descriptionWithNullableFields() { + RequestHandledEvent event = new RequestHandledEvent(this, null, null, 400, null); + assertThat(event.getDescription()).isEqualTo("session=[null]; user=[null]; time=[400ms]"); + } + + @Test + void descriptionWithoutFailure() { + RequestHandledEvent event = new RequestHandledEvent(this, "123-456", "user", 400, null); + assertThat(event.getDescription()).isEqualTo("session=[123-456]; user=[user]; time=[400ms]"); + } + + @Test + void descriptionWithFailure() { + RequestHandledEvent event = new RequestHandledEvent(this, "123-456", "user", 400, + new IllegalStateException("Expected failure")); + assertThat(event.getDescription()).isEqualTo( + "session=[123-456]; user=[user]; time=[400ms]; failure=[java.lang.IllegalStateException: Expected failure]"); + } + +} diff --git a/spring-web/src/test/java/org/springframework/web/context/support/ServletRequestHandledEventTests.java b/spring-web/src/test/java/org/springframework/web/context/support/ServletRequestHandledEventTests.java new file mode 100644 index 000000000000..e9bee46a25b6 --- /dev/null +++ b/spring-web/src/test/java/org/springframework/web/context/support/ServletRequestHandledEventTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.web.context.support; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link ServletRequestHandledEvent}. + * + * @author Stephane Nicoll + */ +class ServletRequestHandledEventTests { + + @Test + void descriptionWithoutStatusCode() { + ServletRequestHandledEvent event = new ServletRequestHandledEvent(this, + "/", "example.com", "GET", "dispatcher", "123-456", "user", 400); + assertThat(event.getDescription()).contains("status=[-1]") + .doesNotContain("failure=["); + } + + @Test + void descriptionWithFailure() { + ServletRequestHandledEvent event = new ServletRequestHandledEvent(this, + "/", "example.com", "GET", "dispatcher", "123-456", "user", + 400, new IllegalStateException("Test"), 500); + assertThat(event.getDescription()).contains("status=[500]", + "failure=[java.lang.IllegalStateException: Test]"); + } + +}