Browse Source

Use connection id in server log messages if possible

Issue: SPR-16966
pull/1870/head
Rossen Stoyanchev 6 years ago
parent
commit
39d5874441
  1. 9
      spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpRequest.java
  2. 10
      spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java
  3. 6
      spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java
  4. 9
      spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java
  5. 17
      spring-web/src/main/java/org/springframework/web/server/adapter/HttpWebHandlerAdapter.java

9
spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpRequest.java

@ -179,4 +179,13 @@ public abstract class AbstractServerHttpRequest implements ServerHttpRequest { @@ -179,4 +179,13 @@ public abstract class AbstractServerHttpRequest implements ServerHttpRequest {
*/
public abstract <T> T getNativeRequest();
/**
* Return an id for the underlying connection, if available.
* @since 5.1
*/
@Nullable
public String getConnectionId() {
return null;
}
}

10
spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java

@ -191,6 +191,9 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder { @@ -191,6 +191,9 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
private final ServerHttpRequest originalRequest;
@Nullable
private final String requestId;
public MutatedServerHttpRequest(URI uri, @Nullable String contextPath,
HttpHeaders headers, String methodValue, MultiValueMap<String, HttpCookie> cookies,
@ -203,6 +206,8 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder { @@ -203,6 +206,8 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
this.sslInfo = sslInfo != null ? sslInfo : originalRequest.getSslInfo();
this.body = body;
this.originalRequest = originalRequest;
this.requestId = originalRequest instanceof AbstractServerHttpRequest ?
((AbstractServerHttpRequest) originalRequest).getConnectionId() : null;
}
@Override
@ -237,6 +242,11 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder { @@ -237,6 +242,11 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
public <T> T getNativeRequest() {
return (T) this.originalRequest;
}
@Override
public String getConnectionId() {
return this.requestId;
}
}
}

6
spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java

@ -174,4 +174,10 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest { @@ -174,4 +174,10 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest {
return (T) this.request;
}
@Override
public String getConnectionId() {
return this.request instanceof Connection ?
((Connection) this.request).channel().id().asShortText() : null;
}
}

9
spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java

@ -44,6 +44,7 @@ import org.springframework.lang.Nullable; @@ -44,6 +44,7 @@ import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
@ -59,6 +60,8 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest { @@ -59,6 +60,8 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
private final RequestBodyPublisher body;
private final String connectionId;
public UndertowServerHttpRequest(HttpServerExchange exchange, DataBufferFactory bufferFactory)
throws URISyntaxException {
@ -67,6 +70,7 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest { @@ -67,6 +70,7 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
this.exchange = exchange;
this.body = new RequestBodyPublisher(exchange, bufferFactory);
this.body.registerListeners(exchange);
this.connectionId = ObjectUtils.getIdentityHexString(this.exchange.getConnection());
}
private static URI initUri(HttpServerExchange exchange) throws URISyntaxException {
@ -127,6 +131,11 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest { @@ -127,6 +131,11 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
return (T) this.exchange;
}
@Override
public String getConnectionId() {
return this.connectionId;
}
private static class RequestBodyPublisher extends AbstractListenerReadPublisher<DataBuffer> {

17
spring-web/src/main/java/org/springframework/web/server/adapter/HttpWebHandlerAdapter.java

@ -29,6 +29,7 @@ import org.springframework.core.NestedExceptionUtils; @@ -29,6 +29,7 @@ import org.springframework.core.NestedExceptionUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.codec.LoggingCodecSupport;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.server.reactive.AbstractServerHttpRequest;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
@ -215,10 +216,7 @@ public class HttpWebHandlerAdapter extends WebHandlerDecorator implements HttpHa @@ -215,10 +216,7 @@ public class HttpWebHandlerAdapter extends WebHandlerDecorator implements HttpHa
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
ServerWebExchange exchange = createExchange(request, response);
String logId = ObjectUtils.getIdentityHexString(request);
exchange.getAttributes().put(ServerWebExchange.LOG_ID_ATTRIBUTE, logId);
exchange.getAttributes().put(ServerWebExchange.LOG_ID_ATTRIBUTE, initLogId(request));
logExchange(exchange);
return getDelegate().handle(exchange)
@ -232,6 +230,17 @@ public class HttpWebHandlerAdapter extends WebHandlerDecorator implements HttpHa @@ -232,6 +230,17 @@ public class HttpWebHandlerAdapter extends WebHandlerDecorator implements HttpHa
getCodecConfigurer(), getLocaleContextResolver(), this.applicationContext);
}
private String initLogId(ServerHttpRequest request) {
String logId = null;
if (request instanceof AbstractServerHttpRequest) {
logId = ((AbstractServerHttpRequest) request).getConnectionId();
}
if (logId == null) {
logId = ObjectUtils.getIdentityHexString(request);
}
return logId;
}
private void logExchange(ServerWebExchange exchange) {
if (logger.isDebugEnabled()) {
String logPrefix = exchange.getLogPrefix();

Loading…
Cancel
Save