3838import com .google .common .collect .ImmutableMap ;
3939import com .google .common .collect .Iterators ;
4040import com .google .common .collect .Sets ;
41+ import com .google .logging .v2 .LogName ;
4142import com .google .protobuf .Any ;
4243import com .google .protobuf .StringValue ;
4344import java .util .Iterator ;
@@ -76,6 +77,16 @@ public abstract class BaseSystemTest {
7677 */
7778 protected abstract String formatForTest (String resourceName );
7879
80+ /**
81+ * Creates an equality expression for logging filter.
82+ *
83+ * @see <a href="https://cloud.google.com/logging/docs/view/advanced_filters">Advanced Logs
84+ * Filters Documentation</a>
85+ */
86+ static <V > String createEqualityFilter (String name , V value ) {
87+ return name + " = " + "\" " + value .toString () + "\" " ;
88+ }
89+
7990 @ Test
8091 public void testCreateGetUpdateAndDeleteSink () {
8192 String name = formatForTest ("test-create-get-update-sink" );
@@ -335,26 +346,27 @@ public void testListMetricsAsync() throws ExecutionException, InterruptedExcepti
335346
336347 @ Test
337348 public void testWriteAndListLogEntries () throws InterruptedException {
338- String logName = formatForTest ("test-write-log-entries-log" );
339- String filter =
340- " logName = projects/" + logging (). getOptions () .getProjectId () + "/logs/" + logName ;
349+ String logId = formatForTest ("test-write-log-entries-log" );
350+ LoggingOptions loggingOptions = logging (). getOptions ();
351+ LogName logName = LogName . create ( loggingOptions .getProjectId (), logId ) ;
341352 StringPayload firstPayload = StringPayload .of ("stringPayload" );
342353 LogEntry firstEntry = LogEntry .newBuilder (firstPayload )
343354 .addLabel ("key1" , "value1" )
344- .setLogName (logName )
355+ .setLogName (logId )
345356 .setHttpRequest (HttpRequest .newBuilder ().setStatus (500 ).build ())
346357 .setResource (MonitoredResource .newBuilder ("global" ).build ())
347358 .build ();
348359 JsonPayload secondPayload =
349360 JsonPayload .of (ImmutableMap .<String , Object >of ("jsonKey" , "jsonValue" ));
350361 LogEntry secondEntry = LogEntry .newBuilder (secondPayload )
351362 .addLabel ("key2" , "value2" )
352- .setLogName (logName )
363+ .setLogName (logId )
353364 .setOperation (Operation .of ("operationId" , "operationProducer" ))
354365 .setResource (MonitoredResource .newBuilder ("cloudsql_database" ).build ())
355366 .build ();
356367 logging ().write (ImmutableList .of (firstEntry ));
357368 logging ().write (ImmutableList .of (secondEntry ));
369+ String filter = createEqualityFilter ("logName" , logName );
358370 EntryListOption [] options = {EntryListOption .filter (filter ), EntryListOption .pageSize (1 )};
359371 Page <LogEntry > page = logging ().listLogEntries (options );
360372 while (Iterators .size (page .iterateAll ().iterator ()) < 2 ) {
@@ -365,7 +377,7 @@ public void testWriteAndListLogEntries() throws InterruptedException {
365377 assertTrue (iterator .hasNext ());
366378 LogEntry entry = iterator .next ();
367379 assertEquals (firstPayload , entry .getPayload ());
368- assertEquals (logName , entry .getLogName ());
380+ assertEquals (logId , entry .getLogName ());
369381 assertEquals (ImmutableMap .of ("key1" , "value1" ), entry .getLabels ());
370382 assertEquals ("global" , entry .getResource ().getType ());
371383 assertEquals (HttpRequest .newBuilder ().setStatus (500 ).build (), entry .getHttpRequest ());
@@ -376,7 +388,7 @@ public void testWriteAndListLogEntries() throws InterruptedException {
376388 assertTrue (iterator .hasNext ());
377389 entry = iterator .next ();
378390 assertEquals (secondPayload , entry .getPayload ());
379- assertEquals (logName , entry .getLogName ());
391+ assertEquals (logId , entry .getLogName ());
380392 assertEquals (ImmutableMap .of ("key2" , "value2" ), entry .getLabels ());
381393 assertEquals ("cloudsql_database" , entry .getResource ().getType ());
382394 assertEquals (Operation .of ("operationId" , "operationProducer" ), entry .getOperation ());
@@ -396,14 +408,14 @@ public void testWriteAndListLogEntries() throws InterruptedException {
396408 while (iterator .hasNext ()) {
397409 assertTrue (iterator .next ().getTimestamp () <= lastTimestamp );
398410 }
399- assertTrue (logging ().deleteLog (logName ));
411+ assertTrue (logging ().deleteLog (logId ));
400412 }
401413
402414 @ Test
403415 public void testWriteAndListLogEntriesAsync () throws InterruptedException , ExecutionException {
404- String logName = formatForTest ("test-write-log-entries-async-log" );
405- String filter =
406- " logName = projects/" + logging (). getOptions () .getProjectId () + "/logs/" + logName ;
416+ String logId = formatForTest ("test-write-log-entries-async-log" );
417+ LoggingOptions loggingOptions = logging (). getOptions ();
418+ LogName logName = LogName . create ( loggingOptions .getProjectId (), logId ) ;
407419 StringPayload firstPayload = StringPayload .of ("stringPayload" );
408420 LogEntry firstEntry = LogEntry .newBuilder (firstPayload ).setSeverity (Severity .ALERT ).build ();
409421 ProtoPayload secondPayload =
@@ -412,8 +424,9 @@ public void testWriteAndListLogEntriesAsync() throws InterruptedException, Execu
412424 logging ().write (ImmutableList .of (firstEntry , secondEntry ),
413425 WriteOption .labels (ImmutableMap .of ("key1" , "value1" )),
414426 WriteOption .resource (MonitoredResource .newBuilder ("global" ).build ()),
415- WriteOption .logName (logName ));
427+ WriteOption .logName (logId ));
416428 logging ().flush ();
429+ String filter = createEqualityFilter ("logName" , logName );
417430 EntryListOption [] options = {EntryListOption .filter (filter ), EntryListOption .pageSize (1 )};
418431 AsyncPage <LogEntry > page = logging ().listLogEntriesAsync (options ).get ();
419432 while (Iterators .size (page .iterateAll ().iterator ()) < 2 ) {
@@ -424,7 +437,7 @@ public void testWriteAndListLogEntriesAsync() throws InterruptedException, Execu
424437 assertTrue (iterator .hasNext ());
425438 LogEntry entry = iterator .next ();
426439 assertEquals (firstPayload , entry .getPayload ());
427- assertEquals (logName , entry .getLogName ());
440+ assertEquals (logId , entry .getLogName ());
428441 assertEquals (ImmutableMap .of ("key1" , "value1" ), entry .getLabels ());
429442 assertEquals ("global" , entry .getResource ().getType ());
430443 assertNull (entry .getHttpRequest ());
@@ -435,51 +448,53 @@ public void testWriteAndListLogEntriesAsync() throws InterruptedException, Execu
435448 assertTrue (iterator .hasNext ());
436449 entry = iterator .next ();
437450 assertEquals (secondPayload , entry .getPayload ());
438- assertEquals (logName , entry .getLogName ());
451+ assertEquals (logId , entry .getLogName ());
439452 assertEquals (ImmutableMap .of ("key1" , "value1" ), entry .getLabels ());
440453 assertEquals ("global" , entry .getResource ().getType ());
441454 assertNull (entry .getOperation ());
442455 assertEquals (Severity .DEBUG , entry .getSeverity ());
443456 assertNull (entry .getHttpRequest ());
444457 assertNotNull (entry .getInsertId ());
445458 assertNotNull (entry .getTimestamp ());
446- assertTrue (logging ().deleteLogAsync (logName ).get ());
459+ assertTrue (logging ().deleteLogAsync (logId ).get ());
447460 }
448461
449462 @ Test
450463 public void testDeleteNonExistingLog () {
451- String logName = formatForTest ("test-delete-non-existing-log" );
452- assertFalse (logging ().deleteLog (logName ));
464+ String logId = formatForTest ("test-delete-non-existing-log" );
465+ assertFalse (logging ().deleteLog (logId ));
453466 }
454467
455468 @ Test
456469 public void testDeleteNonExistingLogAsync () throws ExecutionException , InterruptedException {
457- String logName = formatForTest ("test-delete-non-existing-log-async" );
458- assertFalse (logging ().deleteLogAsync (logName ).get ());
470+ String logId = formatForTest ("test-delete-non-existing-log-async" );
471+ assertFalse (logging ().deleteLogAsync (logId ).get ());
459472 }
460473
461474 @ Test
462475 public void testLoggingHandler () throws InterruptedException {
463- String logName = formatForTest ("test-logging-handler" );
476+ String logId = formatForTest ("test-logging-handler" );
464477 LoggingOptions options = logging ().getOptions ();
465- LoggingHandler handler = new LoggingHandler (logName , options );
478+ LogName logName = LogName .create (options .getProjectId (), logId );
479+ LoggingHandler handler = new LoggingHandler (logId , options );
466480 handler .setLevel (Level .INFO );
467481 Logger logger = Logger .getLogger (getClass ().getName ());
468482 logger .addHandler (handler );
469483 logger .setLevel (Level .INFO );
470484 logger .info ("Message" );
485+ String filter = createEqualityFilter ("logName" , logName );
471486 Iterator <LogEntry > iterator =
472- logging ().listLogEntries (EntryListOption .filter ("logName:" + logName )).iterateAll ().iterator ();
487+ logging ().listLogEntries (EntryListOption .filter (filter )).iterateAll ().iterator ();
473488 while (!iterator .hasNext ()) {
474489 Thread .sleep (500L );
475490 iterator =
476- logging ().listLogEntries (EntryListOption .filter ("logName:" + logName )).iterateAll ().iterator ();
491+ logging ().listLogEntries (EntryListOption .filter (filter )).iterateAll ().iterator ();
477492 }
478493 assertTrue (iterator .hasNext ());
479494 LogEntry entry = iterator .next ();
480495 assertTrue (entry .getPayload () instanceof StringPayload );
481496 assertTrue (entry .<StringPayload >getPayload ().getData ().contains ("Message" ));
482- assertEquals (logName , entry .getLogName ());
497+ assertEquals (logId , entry .getLogName ());
483498 assertEquals (ImmutableMap .of ("levelName" , "INFO" ,
484499 "levelValue" , String .valueOf (Level .INFO .intValue ())), entry .getLabels ());
485500 assertEquals ("global" , entry .getResource ().getType ());
@@ -492,36 +507,38 @@ public void testLoggingHandler() throws InterruptedException {
492507 assertNotNull (entry .getTimestamp ());
493508 assertFalse (iterator .hasNext ());
494509 logger .removeHandler (handler );
495- logging ().deleteLog (logName );
510+ logging ().deleteLog (logId );
496511 }
497512
498513 @ Test
499514 public void testSyncLoggingHandler () throws InterruptedException {
500- String logName = formatForTest ("test-sync-logging-handler" );
515+ String logId = formatForTest ("test-sync-logging-handler" );
501516 LoggingOptions options = logging ().getOptions ();
517+ LogName logName = LogName .create (options .getProjectId (), logId );
502518 MonitoredResource resource = MonitoredResource .of ("gce_instance" ,
503519 ImmutableMap .of ("project_id" , options .getProjectId (),
504520 "instance_id" , "instance" ,
505521 "zone" , "us-central1-a" ));
506- LoggingHandler handler = new LoggingHandler (logName , options , resource );
522+ LoggingHandler handler = new LoggingHandler (logId , options , resource );
507523 handler .setLevel (Level .WARNING );
508524 handler .setSynchronicity (Synchronicity .SYNC );
509525 Logger logger = Logger .getLogger (getClass ().getName ());
510526 logger .addHandler (handler );
511527 logger .setLevel (Level .WARNING );
512528 logger .warning ("Message" );
529+ String filter = createEqualityFilter ("logName" , logName );
513530 Iterator <LogEntry > iterator =
514- logging ().listLogEntries (EntryListOption .filter ("logName:" + logName )).iterateAll ().iterator ();
531+ logging ().listLogEntries (EntryListOption .filter (filter )).iterateAll ().iterator ();
515532 while (!iterator .hasNext ()) {
516533 Thread .sleep (500L );
517534 iterator =
518- logging ().listLogEntries (EntryListOption .filter ("logName:" + logName )).iterateAll ().iterator ();
535+ logging ().listLogEntries (EntryListOption .filter (filter )).iterateAll ().iterator ();
519536 }
520537 assertTrue (iterator .hasNext ());
521538 LogEntry entry = iterator .next ();
522539 assertTrue (entry .getPayload () instanceof StringPayload );
523540 assertTrue (entry .<StringPayload >getPayload ().getData ().contains ("Message" ));
524- assertEquals (logName , entry .getLogName ());
541+ assertEquals (logId , entry .getLogName ());
525542 assertEquals (ImmutableMap .of ("levelName" , "WARNING" ,
526543 "levelValue" , String .valueOf (Level .WARNING .intValue ())), entry .getLabels ());
527544 assertEquals (resource , entry .getResource ());
@@ -532,6 +549,6 @@ public void testSyncLoggingHandler() throws InterruptedException {
532549 assertNotNull (entry .getTimestamp ());
533550 assertFalse (iterator .hasNext ());
534551 logger .removeHandler (handler );
535- logging ().deleteLog (logName );
552+ logging ().deleteLog (logId );
536553 }
537554}
0 commit comments