Skip to content

Commit b641ef7

Browse files
committed
feat: setter for Log of RetryTemplate
1 parent b98b2a5 commit b641ef7

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

src/main/java/org/springframework/retry/support/RetryTemplate.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public class RetryTemplate implements RetryOperations {
8787
*/
8888
private static final String GLOBAL_STATE = "state.global";
8989

90-
protected final Log logger = LogFactory.getLog(getClass());
90+
protected Log logger = LogFactory.getLog(getClass());
9191

9292
private volatile BackOffPolicy backOffPolicy = new NoBackOffPolicy();
9393

@@ -186,6 +186,17 @@ public boolean hasListeners() {
186186
return this.listeners.length > 0;
187187
}
188188

189+
/**
190+
* Setter for {@link Log}. If not applied the following is used:
191+
* <p>
192+
* {@code LogFactory.getLog(getClass())}
193+
* </p>
194+
* @param logger the logger the retry template uses for logging
195+
*/
196+
public void setLogger(Log logger) {
197+
this.logger = logger;
198+
}
199+
189200
/**
190201
* Setter for {@link BackOffPolicy}.
191202
* @param backOffPolicy the {@link BackOffPolicy}

src/main/java/org/springframework/retry/support/RetryTemplateBuilder.java

+25
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.function.Predicate;
2323

24+
import org.apache.commons.logging.Log;
2425
import org.springframework.classify.BinaryExceptionClassifier;
2526
import org.springframework.classify.BinaryExceptionClassifierBuilder;
2627
import org.springframework.retry.RetryListener;
@@ -85,6 +86,8 @@ public class RetryTemplateBuilder {
8586

8687
private RetryPolicy baseRetryPolicy;
8788

89+
private Log logger;
90+
8891
private BackOffPolicy backOffPolicy;
8992

9093
private List<RetryListener> listeners;
@@ -288,6 +291,22 @@ public RetryTemplateBuilder exponentialBackoff(Duration initialInterval, double
288291
return this.exponentialBackoff(initialInterval.toMillis(), multiplier, maxInterval.toMillis(), withRandom);
289292
}
290293

294+
/**
295+
* Applies a dedicated logger to the {@link RetryTemplate}. If not applied the
296+
* following is used:
297+
* <p>
298+
* {@code LogFactory.getLog(getClass())}
299+
* </p>
300+
* @param logger the logger which should be used for logging
301+
* @return this
302+
*/
303+
public RetryTemplateBuilder withLogger(Log logger) {
304+
Assert.isNull(this.logger, "You have already applied a logger");
305+
Assert.notNull(logger, "The given logger should not be null");
306+
this.logger = logger;
307+
return this;
308+
}
309+
291310
/**
292311
* Perform each retry after a fixed amount of time.
293312
* @param interval fixed interval in milliseconds
@@ -584,6 +603,12 @@ public RetryTemplate build() {
584603
finalPolicy.setPolicies(new RetryPolicy[] { this.baseRetryPolicy, exceptionRetryPolicy });
585604
retryTemplate.setRetryPolicy(finalPolicy);
586605

606+
// Logger
607+
608+
if (this.logger != null) {
609+
retryTemplate.setLogger(this.logger);
610+
}
611+
587612
// Backoff policy
588613

589614
if (this.backOffPolicy == null) {

src/test/java/org/springframework/retry/support/RetryTemplateBuilderTests.java

+16
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525
import java.util.function.Predicate;
2626

27+
import org.apache.commons.logging.Log;
2728
import org.junit.jupiter.api.Test;
2829
import org.springframework.classify.BinaryExceptionClassifier;
2930
import org.springframework.retry.RetryListener;
@@ -346,6 +347,21 @@ public void testValidateZeroInitInterval() {
346347
.isThrownBy(() -> RetryTemplate.builder().exponentialBackoff(0, 2, 200).build());
347348
}
348349

350+
@Test
351+
public void testBuilderWithLogger() {
352+
Log logMock = mock(Log.class);
353+
RetryTemplate retryTemplate = RetryTemplate.builder().withLogger(logMock).build();
354+
Log logger = getPropertyValue(retryTemplate, "logger", Log.class);
355+
assertThat(logger).isEqualTo(logMock);
356+
}
357+
358+
@Test
359+
public void testBuilderWithDefaultLogger() {
360+
RetryTemplate retryTemplate = RetryTemplate.builder().build();
361+
Log logger = getPropertyValue(retryTemplate, "logger", Log.class);
362+
assertThat(logger).isNotNull();
363+
}
364+
349365
/* ---------------- Utils -------------- */
350366

351367
private static class PolicyTuple {

src/test/java/org/springframework/retry/support/RetryTemplateTests.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import java.util.concurrent.atomic.AtomicBoolean;
2121
import java.util.concurrent.atomic.AtomicInteger;
2222

23+
import org.apache.commons.logging.Log;
2324
import org.junit.jupiter.api.Test;
2425

26+
import org.mockito.ArgumentCaptor;
2527
import org.springframework.classify.BinaryExceptionClassifier;
2628
import org.springframework.retry.RetryCallback;
2729
import org.springframework.retry.RetryContext;
@@ -45,6 +47,7 @@
4547
import static org.mockito.BDDMockito.given;
4648
import static org.mockito.Mockito.mock;
4749
import static org.mockito.Mockito.verify;
50+
import static org.mockito.Mockito.when;
4851

4952
/**
5053
* @author Rob Harrop
@@ -53,6 +56,7 @@
5356
* @author Henning Pöttker
5457
* @author Emanuele Ivaldi
5558
* @author Morulai Planinski
59+
* @author Tobias Soloschenko
5660
*/
5761
public class RetryTemplateTests {
5862

@@ -286,7 +290,6 @@ public void testRethrowError() {
286290
}
287291
}
288292

289-
@SuppressWarnings("serial")
290293
@Test
291294
public void testFailedPolicy() {
292295
RetryTemplate retryTemplate = new RetryTemplate();
@@ -325,7 +328,6 @@ public void testNoBackOffForRethrownException() {
325328
tested.setRetryPolicy(new SimpleRetryPolicy(1));
326329

327330
BackOffPolicy bop = mock(BackOffPolicy.class);
328-
@SuppressWarnings("serial")
329331
BackOffContext backOffContext = new BackOffContext() {
330332
};
331333
tested.setBackOffPolicy(bop);
@@ -439,4 +441,17 @@ public void backOff(BackOffContext backOffContext) throws BackOffInterruptedExce
439441

440442
}
441443

444+
@Test
445+
public void testLoggingAppliedCorrectly() throws Exception {
446+
ArgumentCaptor<String> logOutputCaptor = ArgumentCaptor.forClass(String.class);
447+
RetryTemplate retryTemplate = new RetryTemplate();
448+
Log logMock = mock(Log.class);
449+
when(logMock.isTraceEnabled()).thenReturn(false);
450+
when(logMock.isDebugEnabled()).thenReturn(true);
451+
retryTemplate.setLogger(logMock);
452+
retryTemplate.execute(new MockRetryCallback());
453+
verify(logMock).debug(logOutputCaptor.capture());
454+
assertThat(logOutputCaptor.getValue()).contains("Retry: count=0");
455+
}
456+
442457
}

0 commit comments

Comments
 (0)