Skip to content

Commit 2bda9a0

Browse files
authored
test: switch all test logging to slf4j instead of j.u.l (googleapis#3108)
OpenTelemetry brought with it slf4j and we already have logback configured for tests. Update our tests to use slf4j directly instead of java.util.logging.
1 parent 5290ab1 commit 2bda9a0

File tree

11 files changed

+80
-76
lines changed

11 files changed

+80
-76
lines changed

google-cloud-storage/src/test/java/com/google/cloud/storage/ITGapicBidiUnbufferedWritableByteChannelTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@
4848
import java.util.Set;
4949
import java.util.concurrent.TimeUnit;
5050
import java.util.function.BiConsumer;
51-
import java.util.logging.Logger;
5251
import java.util.stream.Collector;
5352
import java.util.stream.Collectors;
5453
import org.checkerframework.checker.nullness.qual.NonNull;
5554
import org.junit.Test;
55+
import org.slf4j.Logger;
56+
import org.slf4j.LoggerFactory;
5657

5758
public final class ITGapicBidiUnbufferedWritableByteChannelTest {
5859

@@ -830,7 +831,7 @@ public void incremental_partialSuccess() throws Exception {
830831
}
831832

832833
static class BidiWriteService extends StorageImplBase {
833-
private static final Logger LOGGER = Logger.getLogger(BidiWriteService.class.getName());
834+
private static final Logger LOGGER = LoggerFactory.getLogger(BidiWriteService.class);
834835
private final BiConsumer<StreamObserver<BidiWriteObjectResponse>, List<BidiWriteObjectRequest>>
835836
c;
836837

@@ -878,7 +879,7 @@ private static void logUnexpectedRequest(
878879
.map(l -> l.stream().map(StorageV2ProtoUtils::fmtProto).collect(oneLine))
879880
.collect(joining),
880881
build.stream().map(StorageV2ProtoUtils::fmtProto).collect(oneLine));
881-
LOGGER.warning(msg);
882+
LOGGER.warn(msg);
882883
}
883884

884885
@Override

google-cloud-storage/src/test/java/com/google/cloud/storage/ITGapicUnbufferedWritableByteChannelTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@
5151
import java.util.concurrent.atomic.AtomicBoolean;
5252
import java.util.concurrent.atomic.AtomicInteger;
5353
import java.util.function.BiConsumer;
54-
import java.util.logging.Logger;
5554
import java.util.stream.Collector;
5655
import java.util.stream.Collectors;
5756
import org.junit.Test;
57+
import org.slf4j.Logger;
58+
import org.slf4j.LoggerFactory;
5859

5960
public final class ITGapicUnbufferedWritableByteChannelTest {
6061
private static final Logger LOGGER =
61-
Logger.getLogger(ITGapicUnbufferedWritableByteChannelTest.class.getName());
62+
LoggerFactory.getLogger(ITGapicUnbufferedWritableByteChannelTest.class);
6263

6364
private static final Hasher HASHER = Hasher.enabled();
6465
private static final ChunkSegmenter segmenter =
@@ -202,7 +203,7 @@ public void resumableUpload() throws IOException, InterruptedException, Executio
202203
c.close();
203204
} catch (PermissionDeniedException ignore) {
204205
for (String debugMessage : debugMessages) {
205-
LOGGER.warning(debugMessage);
206+
LOGGER.warn(debugMessage);
206207
}
207208
}
208209
assertThat(result.get()).isEqualTo(resp5);
@@ -331,7 +332,7 @@ public void resumableUpload_finalizeWhenWriteAndCloseCalledEvenWhenQuantumAligne
331332
}
332333

333334
static class DirectWriteService extends StorageImplBase {
334-
private static final Logger LOGGER = Logger.getLogger(DirectWriteService.class.getName());
335+
private static final Logger LOGGER = LoggerFactory.getLogger(DirectWriteService.class);
335336
private final BiConsumer<StreamObserver<WriteObjectResponse>, List<WriteObjectRequest>> c;
336337

337338
private ImmutableList.Builder<WriteObjectRequest> requests;
@@ -368,7 +369,7 @@ private static void logUnexpectedRequest(
368369
.map(l -> l.stream().map(StorageV2ProtoUtils::fmtProto).collect(oneLine))
369370
.collect(joining),
370371
build.stream().map(StorageV2ProtoUtils::fmtProto).collect(oneLine));
371-
LOGGER.warning(msg);
372+
LOGGER.warn(msg);
372373
}
373374

374375
@Override

google-cloud-storage/src/test/java/com/google/cloud/storage/conformance/retry/ITRetryConformanceTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,15 @@
6363
import java.util.Set;
6464
import java.util.function.BiPredicate;
6565
import java.util.function.Predicate;
66-
import java.util.logging.Logger;
6766
import java.util.stream.Collectors;
6867
import org.checkerframework.checker.nullness.qual.Nullable;
6968
import org.junit.After;
7069
import org.junit.AssumptionViolatedException;
7170
import org.junit.Before;
7271
import org.junit.Test;
7372
import org.junit.runner.RunWith;
73+
import org.slf4j.Logger;
74+
import org.slf4j.LoggerFactory;
7475

7576
/**
7677
* Load and dynamically generate a series of test cases to verify if the {@link Storage} and
@@ -86,7 +87,7 @@
8687
@Parameterized(RetryConformanceParameterProvider.class)
8788
@ParallelFriendly
8889
public class ITRetryConformanceTest {
89-
private static final Logger LOGGER = Logger.getLogger(ITRetryConformanceTest.class.getName());
90+
private static final Logger LOGGER = LoggerFactory.getLogger(ITRetryConformanceTest.class);
9091

9192
private RetryTestFixture retryTestFixture;
9293

@@ -100,7 +101,7 @@ public class ITRetryConformanceTest {
100101

101102
@Before
102103
public void setUp() throws Throwable {
103-
LOGGER.fine("Running setup...");
104+
LOGGER.trace("Running setup...");
104105
retryTestFixture = retryParameter.retryTestFixture;
105106
testRetryConformance = retryParameter.testRetryConformance;
106107
mapping = retryParameter.rpcMethodMapping;
@@ -111,20 +112,20 @@ public void setUp() throws Throwable {
111112
// the case setup fails for some reason
112113
ctx = ctx(nonTestStorage, empty());
113114
ctx = mapping.getSetup().apply(ctx, testRetryConformance).leftMap(s -> testStorage);
114-
LOGGER.fine("Running setup complete");
115+
LOGGER.trace("Running setup complete");
115116
}
116117

117118
@After
118119
public void tearDown() throws Throwable {
119-
LOGGER.fine("Running teardown...");
120+
LOGGER.trace("Running teardown...");
120121
if (ctx != null) {
121122
ctx = ctx.leftMap(s -> nonTestStorage);
122123
getReplaceStorageInObjectsFromCtx()
123124
.andThen(mapping.getTearDown())
124125
.apply(ctx, testRetryConformance);
125126
}
126127
retryTestFixture.finished(null);
127-
LOGGER.fine("Running teardown complete");
128+
LOGGER.trace("Running teardown complete");
128129
}
129130

130131
/**
@@ -133,7 +134,7 @@ public void tearDown() throws Throwable {
133134
*/
134135
@Test
135136
public void test() throws Throwable {
136-
LOGGER.fine("Running test...");
137+
LOGGER.trace("Running test...");
137138
assertThat(ctx).isNotNull();
138139
try {
139140
ctx =
@@ -146,7 +147,7 @@ public void test() throws Throwable {
146147
retryTestFixture.failed(e, null);
147148
throw e;
148149
}
149-
LOGGER.fine("Running test complete");
150+
LOGGER.trace("Running test complete");
150151
}
151152

152153
/**
@@ -374,7 +375,7 @@ private void validateGeneratedTestCases(
374375
.collect(Collectors.toSet()));
375376

376377
if (!unusedMappings.isEmpty()) {
377-
LOGGER.warning(
378+
LOGGER.warn(
378379
String.format(
379380
Locale.US,
380381
"Declared but unused mappings with ids: [%s]",

google-cloud-storage/src/test/java/com/google/cloud/storage/conformance/retry/RetryTestFixture.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
import com.google.common.collect.ImmutableMap;
3030
import java.io.IOException;
3131
import java.util.Locale;
32-
import java.util.logging.Logger;
3332
import org.junit.AssumptionViolatedException;
3433
import org.junit.rules.TestRule;
3534
import org.junit.rules.TestWatcher;
3635
import org.junit.runner.Description;
36+
import org.slf4j.Logger;
37+
import org.slf4j.LoggerFactory;
3738

3839
/**
3940
* A JUnit 4 {@link TestRule} which integrates with {@link TestBench} and {@link
@@ -44,7 +45,7 @@
4445
* <p>Provides pre-configured instances of {@link Storage} for setup/teardown & test.
4546
*/
4647
final class RetryTestFixture extends TestWatcher {
47-
private static final Logger LOGGER = Logger.getLogger(RetryTestFixture.class.getName());
48+
private static final Logger LOGGER = LoggerFactory.getLogger(RetryTestFixture.class);
4849
private static final int STATUS_CODE_NOT_IMPLEMENTED = 501;
4950

5051
private final CleanupStrategy cleanupStrategy;
@@ -83,7 +84,7 @@ public Storage getTestStorage() {
8384

8485
@Override
8586
protected void starting(Description description) {
86-
LOGGER.fine("Setting up retry_test resource...");
87+
LOGGER.trace("Setting up retry_test resource...");
8788
RetryTestResource retryTestResource =
8889
RetryTestResource.newRetryTestResource(
8990
testRetryConformance.getMethod(),
@@ -93,20 +94,26 @@ protected void starting(Description description) {
9394
retryTest = testBench.createRetryTest(retryTestResource);
9495
} catch (HttpResponseException e) {
9596
if (e.getStatusCode() == STATUS_CODE_NOT_IMPLEMENTED) {
96-
throw new AssumptionViolatedException(
97-
"Testbench not yet implemented for " + retryTestResource);
97+
AssumptionViolatedException exception =
98+
new AssumptionViolatedException(
99+
"Testbench not yet implemented for " + retryTestResource);
100+
// make skips due to not implemented more terse
101+
// we know where this comes from, we don't need the full stack trace for each of the
102+
// 200+ occurrences.
103+
exception.setStackTrace(new StackTraceElement[0]);
104+
throw exception;
98105
} else {
99106
throw new RuntimeException(e);
100107
}
101108
} catch (IOException e) {
102109
throw new RuntimeException(e);
103110
}
104-
LOGGER.fine("Setting up retry_test resource complete");
111+
LOGGER.trace("Setting up retry_test resource complete");
105112
}
106113

107114
@Override
108115
protected void finished(Description description) {
109-
LOGGER.fine("Verifying end state of retry_test resource...");
116+
LOGGER.trace("Verifying end state of retry_test resource...");
110117
try (Storage ignore1 = nonTestStorage;
111118
Storage ignore2 = testStorage) { // use try-with to shut down grpc resources
112119
try {
@@ -117,7 +124,7 @@ protected void finished(Description description) {
117124
}
118125
}
119126
} finally {
120-
LOGGER.fine("Verifying end state of retry_test resource complete");
127+
LOGGER.trace("Verifying end state of retry_test resource complete");
121128
if ((shouldCleanup(testSuccess, testSkipped)) && retryTest != null) {
122129
testBench.deleteRetryTest(retryTest);
123130
retryTest = null;

google-cloud-storage/src/test/java/com/google/cloud/storage/conformance/retry/RpcMethodMappings.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@
105105
import java.util.OptionalInt;
106106
import java.util.Set;
107107
import java.util.concurrent.TimeUnit;
108-
import java.util.logging.Logger;
109108
import java.util.stream.Collectors;
109+
import org.slf4j.Logger;
110+
import org.slf4j.LoggerFactory;
110111

111112
/**
112113
* A class which serves to try and organize all of the {@link RpcMethodMapping} for the retry
@@ -120,7 +121,7 @@
120121
@Immutable
121122
@SuppressWarnings("Guava")
122123
final class RpcMethodMappings {
123-
private static final Logger LOGGER = Logger.getLogger(RpcMethodMappings.class.getName());
124+
private static final Logger LOGGER = LoggerFactory.getLogger(RpcMethodMappings.class);
124125

125126
private static final Predicate<TestRetryConformance> groupIsDownload =
126127
methodGroupIs("storage.objects.download");
@@ -224,7 +225,7 @@ private void validateMappingDefinitions(ArrayList<RpcMethodMapping> a) {
224225

225226
private void reportMappingSummary() {
226227
int mappingCount = funcMap.values().stream().mapToInt(m -> 1).sum();
227-
LOGGER.info("Current total number of mappings defined: " + mappingCount);
228+
LOGGER.info("Current total number of mappings defined: {}", mappingCount);
228229
String counts =
229230
funcMap.asMap().entrySet().stream()
230231
.map(
@@ -244,7 +245,7 @@ private void reportMappingSummary() {
244245
})
245246
.sorted()
246247
.collect(Collectors.joining("\n", "\n", ""));
247-
LOGGER.info("Current number of mappings per rpc method: " + counts);
248+
LOGGER.info("Current number of mappings per rpc method: {}", counts);
248249
OptionalInt max =
249250
funcMap.values().stream().map(RpcMethodMapping::getMappingId).mapToInt(i -> i).max();
250251
if (max.isPresent()) {

google-cloud-storage/src/test/java/com/google/cloud/storage/it/BucketCleaner.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@
4242
import java.util.Comparator;
4343
import java.util.List;
4444
import java.util.Locale;
45-
import java.util.logging.Level;
46-
import java.util.logging.Logger;
4745
import java.util.stream.Collectors;
4846
import java.util.stream.Stream;
4947
import java.util.stream.StreamSupport;
48+
import org.slf4j.Logger;
49+
import org.slf4j.LoggerFactory;
5050

5151
public final class BucketCleaner {
5252

53-
private static final Logger LOGGER = Logger.getLogger(BucketCleaner.class.getName());
53+
private static final Logger LOGGER = LoggerFactory.getLogger(BucketCleaner.class);
5454

5555
public static void doCleanup(String bucketName, Storage s) {
56-
LOGGER.fine("Starting bucket cleanup...");
56+
LOGGER.trace("Starting bucket cleanup...");
5757
String projectId = s.getOptions().getProjectId();
5858
try {
5959
// TODO: probe bucket existence, a bad test could have deleted the bucket
@@ -73,16 +73,16 @@ public static void doCleanup(String bucketName, Storage s) {
7373
if (!anyFailedObjectDeletes) {
7474
s.delete(bucketName, BucketSourceOption.userProject(projectId));
7575
} else {
76-
LOGGER.warning("Unable to delete bucket due to previous failed object deletes");
76+
LOGGER.warn("Unable to delete bucket due to previous failed object deletes");
7777
}
78-
LOGGER.fine("Bucket cleanup complete");
78+
LOGGER.trace("Bucket cleanup complete");
7979
} catch (Exception e) {
80-
LOGGER.log(Level.SEVERE, e, () -> "Error during bucket cleanup.");
80+
LOGGER.error("Error during bucket cleanup.");
8181
}
8282
}
8383

8484
public static void doCleanup(String bucketName, Storage s, StorageControlClient ctrl) {
85-
LOGGER.warning("Starting bucket cleanup: " + bucketName);
85+
LOGGER.warn("Starting bucket cleanup: {}", bucketName);
8686
String projectId = s.getOptions().getProjectId();
8787
try {
8888
// TODO: probe bucket existence, a bad test could have deleted the bucket
@@ -140,7 +140,7 @@ public static void doCleanup(String bucketName, Storage s, StorageControlClient
140140
folder -> {
141141
String formatted =
142142
String.format(Locale.US, "folder = %s", folder.getName());
143-
LOGGER.warning(formatted);
143+
LOGGER.warn(formatted);
144144
boolean success = true;
145145
try {
146146
ctrl.deleteFolderCallable()
@@ -176,7 +176,7 @@ public static void doCleanup(String bucketName, Storage s, StorageControlClient
176176
managedFolder -> {
177177
String formatted =
178178
String.format(Locale.US, "managedFolder = %s", managedFolder.getName());
179-
LOGGER.warning(formatted);
179+
LOGGER.warn(formatted);
180180
boolean success = true;
181181
try {
182182
ctrl.deleteManagedFolderCallable()
@@ -214,17 +214,17 @@ public static void doCleanup(String bucketName, Storage s, StorageControlClient
214214
if (!anyFailedObjectDelete && !anyFailedFolderDelete && !anyFailedManagedFolderDelete) {
215215
s.delete(bucketName, BucketSourceOption.userProject(projectId));
216216
} else {
217-
LOGGER.warning(
217+
LOGGER.warn(
218218
String.format(
219219
Locale.US,
220220
"Unable to delete bucket %s due to previous failed %s deletes",
221221
bucketName,
222222
failed));
223223
}
224224

225-
LOGGER.warning("Bucket cleanup complete: " + bucketName);
225+
LOGGER.warn("Bucket cleanup complete: {}", bucketName);
226226
} catch (Exception e) {
227-
LOGGER.log(Level.SEVERE, e, () -> "Error during bucket cleanup.");
227+
LOGGER.error("Error during bucket cleanup.");
228228
}
229229
}
230230

@@ -234,7 +234,7 @@ private static boolean getIfAnyFailedAndReport(
234234
deleteResults.stream().filter(r -> !r.success).collect(Collectors.toList());
235235
failedDeletes.forEach(
236236
r ->
237-
LOGGER.warning(
237+
LOGGER.warn(
238238
String.format(
239239
Locale.US, "Failed to delete %s %s/%s", resourceType, bucketName, r.name)));
240240
return !failedDeletes.isEmpty();

0 commit comments

Comments
 (0)