Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.

Commit 5f484a7

Browse files
authored
chore: add unit test for LoggingHandler (#813)
rename unit tests to reflect the right configuration name being tested. test log enhancers when redirecting to stdout.
1 parent 749b9d2 commit 5f484a7

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.easymock.EasyMock.replay;
2222
import static org.easymock.EasyMock.reset;
2323
import static org.easymock.EasyMock.verify;
24+
import static org.junit.Assert.assertEquals;
2425
import static org.junit.Assert.assertTrue;
2526

2627
import com.google.api.client.util.Strings;
@@ -356,6 +357,34 @@ public void enhanceLogEntry(Builder builder) {
356357
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
357358
}
358359

360+
@Test
361+
public void testEnhancedLogEntryPrintToStdout() {
362+
final String ExpectedOutput =
363+
"{\"severity\":\"INFO\",\"timestamp\":\"1970-01-02T10:17:36.789Z\",\"logging.googleapis.com/labels\":{\"enhanced\":\"true\"},\"logging.googleapis.com/trace_sampled\":false,\"message\":\"message\"}";
364+
replay(options, logging);
365+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
366+
PrintStream out = new PrintStream(bout);
367+
System.setOut(out);
368+
369+
LoggingEnhancer enhancer =
370+
new LoggingEnhancer() {
371+
@Override
372+
public void enhanceLogEntry(Builder builder) {
373+
builder.addLabel("enhanced", "true");
374+
}
375+
};
376+
LoggingHandler handler =
377+
new LoggingHandler(
378+
LOG_NAME, options, DEFAULT_RESOURCE, Collections.singletonList(enhancer));
379+
handler.setLevel(Level.ALL);
380+
handler.setFormatter(new TestFormatter());
381+
handler.setRedirectToStdout(true);
382+
handler.publish(newLogRecord(Level.INFO, MESSAGE));
383+
384+
assertEquals(ExpectedOutput, bout.toString().trim()); // ignore trailing newline!
385+
System.setOut(null);
386+
}
387+
359388
@Test
360389
public void testTraceEnhancedLogEntry() {
361390
logging.write(ImmutableList.of(TRACE_ENTRY), DEFAULT_OPTIONS);
@@ -545,7 +574,7 @@ public void testAutoPopulationEnabled() {
545574
}
546575

547576
@Test
548-
public void testStructuredLoggingEnabled() {
577+
public void testRedirectToStdoutEnabled() {
549578
setupOptionsToEnableAutoPopulation();
550579
expect(
551580
logging.populateMetadata(
@@ -572,7 +601,7 @@ public void testStructuredLoggingEnabled() {
572601

573602
@Test
574603
/** Validate that nothing is printed to STDOUT */
575-
public void testStructuredLoggingDisabled() {
604+
public void testRedirectToStdoutDisabled() {
576605
ByteArrayOutputStream bout = new ByteArrayOutputStream();
577606
PrintStream out = new PrintStream(bout);
578607
System.setOut(out);

0 commit comments

Comments
 (0)