Skip to content

Commit

Permalink
Only log status in ServletRequestHandledEvent
Browse files Browse the repository at this point in the history
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
  • Loading branch information
snicoll committed Jan 4, 2024
1 parent be9ee91 commit 2784f60
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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]");
}

}
Original file line number Diff line number Diff line change
@@ -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]");
}

}

0 comments on commit 2784f60

Please sign in to comment.