Browse Source

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
pull/31947/head
Stéphane Nicoll 2 years ago
parent
commit
2784f6008e
  1. 13
      spring-web/src/main/java/org/springframework/web/context/support/RequestHandledEvent.java
  2. 3
      spring-web/src/main/java/org/springframework/web/context/support/ServletRequestHandledEvent.java
  3. 50
      spring-web/src/test/java/org/springframework/web/context/support/RequestHandledEventTests.java
  4. 47
      spring-web/src/test/java/org/springframework/web/context/support/ServletRequestHandledEventTests.java

13
spring-web/src/main/java/org/springframework/web/context/support/RequestHandledEvent.java

@ -1,5 +1,5 @@ @@ -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 class RequestHandledEvent extends ApplicationEvent { @@ -146,15 +146,10 @@ public class RequestHandledEvent extends ApplicationEvent {
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();
}

3
spring-web/src/main/java/org/springframework/web/context/support/ServletRequestHandledEvent.java

@ -1,5 +1,5 @@ @@ -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 class ServletRequestHandledEvent extends RequestHandledEvent { @@ -174,6 +174,7 @@ public class ServletRequestHandledEvent extends RequestHandledEvent {
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();

50
spring-web/src/test/java/org/springframework/web/context/support/RequestHandledEventTests.java

@ -0,0 +1,50 @@ @@ -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]");
}
}

47
spring-web/src/test/java/org/springframework/web/context/support/ServletRequestHandledEventTests.java

@ -0,0 +1,47 @@ @@ -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]");
}
}
Loading…
Cancel
Save