Skip to content

Commit 900bc8a

Browse files
committed
Logging improvements for WebFlux
Issue: SPR-16898
1 parent eaffcbe commit 900bc8a

File tree

109 files changed

+1097
-662
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+1097
-662
lines changed

spring-core/src/main/java/org/springframework/core/codec/AbstractDecoder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,8 @@
2020
import java.util.List;
2121
import java.util.Map;
2222

23+
import org.apache.commons.logging.Log;
24+
import org.apache.commons.logging.LogFactory;
2325
import org.reactivestreams.Publisher;
2426
import reactor.core.publisher.Mono;
2527

@@ -37,6 +39,8 @@
3739
*/
3840
public abstract class AbstractDecoder<T> implements Decoder<T> {
3941

42+
protected final Log logger = LogFactory.getLog(getClass());
43+
4044
private final List<MimeType> decodableMimeTypes;
4145

4246

spring-core/src/main/java/org/springframework/core/codec/AbstractEncoder.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,10 @@
1818

1919
import java.util.Arrays;
2020
import java.util.List;
21+
import java.util.Map;
22+
23+
import org.apache.commons.logging.Log;
24+
import org.apache.commons.logging.LogFactory;
2125

2226
import org.springframework.core.ResolvableType;
2327
import org.springframework.lang.Nullable;
@@ -32,6 +36,8 @@
3236
*/
3337
public abstract class AbstractEncoder<T> implements Encoder<T> {
3438

39+
protected final Log logger = LogFactory.getLog(getClass());
40+
3541
private final List<MimeType> encodableMimeTypes;
3642

3743

@@ -53,4 +59,17 @@ public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType
5359
return this.encodableMimeTypes.stream().anyMatch(candidate -> candidate.isCompatibleWith(mimeType));
5460
}
5561

62+
/**
63+
* Helper method to obtain the logger to use from the Map of hints, or fall
64+
* back on the default logger. This may be used for example to override
65+
* logging, e.g. for a multipart request where the full map of part values
66+
* has already been logged.
67+
* @param hints the hints passed to the encode method
68+
* @return the logger to use
69+
* @since 5.1
70+
*/
71+
protected Log getLogger(@Nullable Map<String, Object> hints) {
72+
return hints != null ? ((Log) hints.getOrDefault(Log.class.getName(), logger)) : logger;
73+
}
74+
5675
}

spring-core/src/main/java/org/springframework/core/codec/ByteArrayDecoder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,6 +53,9 @@ protected byte[] decodeDataBuffer(DataBuffer dataBuffer, ResolvableType elementT
5353
byte[] result = new byte[dataBuffer.readableByteCount()];
5454
dataBuffer.read(result);
5555
DataBufferUtils.release(dataBuffer);
56+
if (logger.isDebugEnabled()) {
57+
logger.debug("Read " + result.length + " bytes");
58+
}
5659
return result;
5760
}
5861

spring-core/src/main/java/org/springframework/core/codec/ByteArrayEncoder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Map;
2020

21+
import org.apache.commons.logging.Log;
2122
import org.reactivestreams.Publisher;
2223
import reactor.core.publisher.Flux;
2324

@@ -52,7 +53,14 @@ public Flux<DataBuffer> encode(Publisher<? extends byte[]> inputStream,
5253
DataBufferFactory bufferFactory, ResolvableType elementType, @Nullable MimeType mimeType,
5354
@Nullable Map<String, Object> hints) {
5455

55-
return Flux.from(inputStream).map(bufferFactory::wrap);
56+
return Flux.from(inputStream).map(bytes -> {
57+
DataBuffer dataBuffer = bufferFactory.wrap(bytes);
58+
Log logger = getLogger(hints);
59+
if (logger.isDebugEnabled()) {
60+
logger.debug("Writing " + dataBuffer.readableByteCount() + " bytes");
61+
}
62+
return dataBuffer;
63+
});
5664
}
5765

5866
}

spring-core/src/main/java/org/springframework/core/codec/ByteBufferDecoder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType
5252
protected ByteBuffer decodeDataBuffer(DataBuffer dataBuffer, ResolvableType elementType,
5353
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
5454

55-
ByteBuffer copy = ByteBuffer.allocate(dataBuffer.readableByteCount());
55+
int byteCount = dataBuffer.readableByteCount();
56+
ByteBuffer copy = ByteBuffer.allocate(byteCount);
5657
copy.put(dataBuffer.asByteBuffer());
5758
copy.flip();
5859
DataBufferUtils.release(dataBuffer);
60+
if (logger.isDebugEnabled()) {
61+
logger.debug("Read " + byteCount + " bytes");
62+
}
5963
return copy;
6064
}
6165

spring-core/src/main/java/org/springframework/core/codec/ByteBufferEncoder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.nio.ByteBuffer;
2020
import java.util.Map;
2121

22+
import org.apache.commons.logging.Log;
2223
import org.reactivestreams.Publisher;
2324
import reactor.core.publisher.Flux;
2425

@@ -53,7 +54,14 @@ public Flux<DataBuffer> encode(Publisher<? extends ByteBuffer> inputStream,
5354
DataBufferFactory bufferFactory, ResolvableType elementType, @Nullable MimeType mimeType,
5455
@Nullable Map<String, Object> hints) {
5556

56-
return Flux.from(inputStream).map(bufferFactory::wrap);
57+
return Flux.from(inputStream).map(byteBuffer -> {
58+
DataBuffer dataBuffer = bufferFactory.wrap(byteBuffer);
59+
Log logger = getLogger(hints);
60+
if (logger.isDebugEnabled()) {
61+
logger.debug("Writing " + dataBuffer.readableByteCount() + " bytes");
62+
}
63+
return dataBuffer;
64+
});
5765
}
5866

5967
}

spring-core/src/main/java/org/springframework/core/codec/CharSequenceEncoder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.charset.StandardCharsets;
2323
import java.util.Map;
2424

25+
import org.apache.commons.logging.Log;
2526
import org.reactivestreams.Publisher;
2627
import reactor.core.publisher.Flux;
2728

@@ -65,6 +66,10 @@ public Flux<DataBuffer> encode(Publisher<? extends CharSequence> inputStream,
6566
Charset charset = getCharset(mimeType);
6667

6768
return Flux.from(inputStream).map(charSequence -> {
69+
Log logger = getLogger(hints);
70+
if (logger.isDebugEnabled()) {
71+
logger.debug("Writing '" + charSequence + "'");
72+
}
6873
CharBuffer charBuffer = CharBuffer.wrap(charSequence);
6974
ByteBuffer byteBuffer = charset.encode(charBuffer);
7075
return bufferFactory.wrap(byteBuffer);

spring-core/src/main/java/org/springframework/core/codec/DataBufferDecoder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public Flux<DataBuffer> decode(Publisher<DataBuffer> inputStream, ResolvableType
6262
protected DataBuffer decodeDataBuffer(DataBuffer buffer, ResolvableType elementType,
6363
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
6464

65+
if (logger.isDebugEnabled()) {
66+
logger.debug("Read " + buffer.readableByteCount() + " bytes");
67+
}
6568
return buffer;
6669
}
6770

spring-core/src/main/java/org/springframework/core/codec/DataBufferEncoder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Map;
2020

21+
import org.apache.commons.logging.Log;
2122
import org.reactivestreams.Publisher;
2223
import reactor.core.publisher.Flux;
2324

@@ -52,7 +53,14 @@ public Flux<DataBuffer> encode(Publisher<? extends DataBuffer> inputStream,
5253
DataBufferFactory bufferFactory, ResolvableType elementType, @Nullable MimeType mimeType,
5354
@Nullable Map<String, Object> hints) {
5455

55-
return Flux.from(inputStream);
56+
Flux<DataBuffer> flux = Flux.from(inputStream);
57+
58+
Log logger = getLogger(hints);
59+
if (logger.isDebugEnabled()) {
60+
flux = flux.doOnNext(buffer -> logger.debug("Writing " + buffer.readableByteCount() + " bytes"));
61+
}
62+
63+
return flux;
5664
}
5765

5866
}

spring-core/src/main/java/org/springframework/core/codec/ResourceDecoder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ protected Resource decodeDataBuffer(DataBuffer dataBuffer, ResolvableType elemen
7272
Class<?> clazz = elementType.getRawClass();
7373
Assert.state(clazz != null, "No resource class");
7474

75+
if (logger.isDebugEnabled()) {
76+
logger.debug("Read " + bytes.length + " bytes");
77+
}
78+
7579
if (InputStreamResource.class == clazz) {
7680
return new InputStreamResource(new ByteArrayInputStream(bytes));
7781
}

0 commit comments

Comments
 (0)