1717package com .google .cloud .logging ;
1818
1919import static com .google .api .client .util .Preconditions .checkArgument ;
20+ import static com .google .cloud .logging .Logging .EntryListOption .OptionType .FILTER ;
21+ import static com .google .cloud .logging .Logging .EntryListOption .OptionType .ORDER_BY ;
2022import static com .google .cloud .logging .Logging .ListOption .OptionType .PAGE_SIZE ;
2123import static com .google .cloud .logging .Logging .ListOption .OptionType .PAGE_TOKEN ;
24+ import static com .google .cloud .logging .Logging .WriteOption .OptionType .LABELS ;
25+ import static com .google .cloud .logging .Logging .WriteOption .OptionType .LOG_NAME ;
26+ import static com .google .cloud .logging .Logging .WriteOption .OptionType .RESOURCE ;
2227import static com .google .common .util .concurrent .Futures .lazyTransform ;
2328
2429import com .google .cloud .AsyncPage ;
2530import com .google .cloud .AsyncPageImpl ;
2631import com .google .cloud .BaseService ;
32+ import com .google .cloud .MonitoredResource ;
2733import com .google .cloud .MonitoredResourceDescriptor ;
2834import com .google .cloud .Page ;
2935import com .google .cloud .PageImpl ;
3440import com .google .common .base .Function ;
3541import com .google .common .base .Throwables ;
3642import com .google .common .collect .ImmutableList ;
43+ import com .google .common .collect .Iterables ;
3744import com .google .common .collect .Lists ;
3845import com .google .common .collect .Maps ;
3946import com .google .common .util .concurrent .Uninterruptibles ;
4451import com .google .logging .v2 .DeleteSinkRequest ;
4552import com .google .logging .v2 .GetLogMetricRequest ;
4653import com .google .logging .v2 .GetSinkRequest ;
54+ import com .google .logging .v2 .ListLogEntriesRequest ;
55+ import com .google .logging .v2 .ListLogEntriesResponse ;
4756import com .google .logging .v2 .ListLogMetricsRequest ;
4857import com .google .logging .v2 .ListLogMetricsResponse ;
4958import com .google .logging .v2 .ListMonitoredResourceDescriptorsRequest ;
5261import com .google .logging .v2 .ListSinksResponse ;
5362import com .google .logging .v2 .UpdateLogMetricRequest ;
5463import com .google .logging .v2 .UpdateSinkRequest ;
64+ import com .google .logging .v2 .WriteLogEntriesRequest ;
65+ import com .google .logging .v2 .WriteLogEntriesResponse ;
5566import com .google .protobuf .Empty ;
5667
5768import java .util .List ;
@@ -71,6 +82,13 @@ public Boolean apply(Empty input) {
7182 return input != null ;
7283 }
7384 };
85+ private static final Function <WriteLogEntriesResponse , Void > WRITE_RESPONSE_TO_VOID_FUNCTION =
86+ new Function <WriteLogEntriesResponse , Void >() {
87+ @ Override
88+ public Void apply (WriteLogEntriesResponse input ) {
89+ return null ;
90+ }
91+ };
7492
7593 LoggingImpl (LoggingOptions options ) {
7694 super (options );
@@ -154,6 +172,21 @@ public Future<AsyncPage<Metric>> nextPage() {
154172 }
155173 }
156174
175+ private static class LogEntryPageFetcher extends BasePageFetcher <LogEntry > {
176+
177+ private static final long serialVersionUID = 4001239712280747734L ;
178+
179+ LogEntryPageFetcher (LoggingOptions serviceOptions , String cursor ,
180+ Map <Option .OptionType , ?> requestOptions ) {
181+ super (serviceOptions , cursor , requestOptions );
182+ }
183+
184+ @ Override
185+ public Future <AsyncPage <LogEntry >> nextPage () {
186+ return listLogEntriesAsync (serviceOptions (), requestOptions ());
187+ }
188+ }
189+
157190 @ Override
158191 public Sink create (SinkInfo sink ) {
159192 return get (createAsync (sink ));
@@ -408,6 +441,87 @@ public Future<Boolean> deleteMetricAsync(String metric) {
408441 return lazyTransform (rpc .delete (request ), EMPTY_TO_BOOLEAN_FUNCTION );
409442 }
410443
444+ private static WriteLogEntriesRequest writeLogEntriesRequest (LoggingOptions serviceOptions ,
445+ Iterable <LogEntry > logEntries , Map <Option .OptionType , ?> options ) {
446+ String projectId = serviceOptions .projectId ();
447+ WriteLogEntriesRequest .Builder builder = WriteLogEntriesRequest .newBuilder ();
448+ String logName = LOG_NAME .get (options );
449+ if (logName != null ) {
450+ builder .setLogName (LoggingServiceV2Api .formatLogName (projectId , logName ));
451+ }
452+ MonitoredResource resource = RESOURCE .get (options );
453+ if (resource != null ) {
454+ builder .setResource (resource .toPb ());
455+ }
456+ Map <String , String > labels = LABELS .get (options );
457+ if (labels != null ) {
458+ builder .putAllLabels (labels );
459+ }
460+ builder .addAllEntries (Iterables .transform (logEntries , LogEntry .toPbFunction (projectId )));
461+ return builder .build ();
462+ }
463+
464+ public void write (Iterable <LogEntry > logEntries , WriteOption ... options ) {
465+ get (writeAsync (logEntries , options ));
466+ }
467+
468+ public Future <Void > writeAsync (Iterable <LogEntry > logEntries , WriteOption ... options ) {
469+ return lazyTransform (
470+ rpc .write (writeLogEntriesRequest (options (), logEntries , optionMap (options ))),
471+ WRITE_RESPONSE_TO_VOID_FUNCTION );
472+ }
473+
474+ private static ListLogEntriesRequest listLogEntriesRequest (LoggingOptions serviceOptions ,
475+ Map <Option .OptionType , ?> options ) {
476+ ListLogEntriesRequest .Builder builder = ListLogEntriesRequest .newBuilder ();
477+ builder .addProjectIds (serviceOptions .projectId ());
478+ Integer pageSize = PAGE_SIZE .get (options );
479+ if (pageSize != null ) {
480+ builder .setPageSize (pageSize );
481+ }
482+ String pageToken = PAGE_TOKEN .get (options );
483+ if (pageToken != null ) {
484+ builder .setPageToken (pageToken );
485+ }
486+ String orderBy = ORDER_BY .get (options );
487+ if (orderBy != null ) {
488+ builder .setOrderBy (orderBy );
489+ }
490+ String filter = FILTER .get (options );
491+ if (filter != null ) {
492+ builder .setFilter (filter );
493+ }
494+ return builder .build ();
495+ }
496+
497+ private static Future <AsyncPage <LogEntry >> listLogEntriesAsync (
498+ final LoggingOptions serviceOptions , final Map <Option .OptionType , ?> options ) {
499+ final ListLogEntriesRequest request = listLogEntriesRequest (serviceOptions , options );
500+ Future <ListLogEntriesResponse > list = serviceOptions .rpc ().list (request );
501+ return lazyTransform (list , new Function <ListLogEntriesResponse , AsyncPage <LogEntry >>() {
502+ @ Override
503+ public AsyncPage <LogEntry > apply (ListLogEntriesResponse listLogEntrysResponse ) {
504+ List <LogEntry > entries = listLogEntrysResponse .getEntriesList () == null
505+ ? ImmutableList .<LogEntry >of () : Lists .transform (listLogEntrysResponse .getEntriesList (),
506+ LogEntry .FROM_PB_FUNCTION );
507+ String cursor = listLogEntrysResponse .getNextPageToken ().equals ("" ) ? null
508+ : listLogEntrysResponse .getNextPageToken ();
509+ return new AsyncPageImpl <>(new LogEntryPageFetcher (serviceOptions , cursor , options ), cursor ,
510+ entries );
511+ }
512+ });
513+ }
514+
515+ @ Override
516+ public Page <LogEntry > listLogEntries (EntryListOption ... options ) {
517+ return get (listLogEntriesAsync (options ));
518+ }
519+
520+ @ Override
521+ public Future <AsyncPage <LogEntry >> listLogEntriesAsync (EntryListOption ... options ) {
522+ return listLogEntriesAsync (options (), optionMap (options ));
523+ }
524+
411525 @ Override
412526 public void close () throws Exception {
413527 if (closed ) {
0 commit comments