2121import static org .easymock .EasyMock .replay ;
2222import static org .easymock .EasyMock .reset ;
2323import static org .easymock .EasyMock .verify ;
24+ import static org .junit .Assert .assertEquals ;
2425import static org .junit .Assert .assertTrue ;
2526
2627import 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