public interface Logging extends AutoCloseable, Service<LoggingOptions>
Modifier and Type | Interface and Description |
---|---|
static class |
Logging.EntryListOption
Class for specifying options for listing log entries.
|
static class |
Logging.ListOption
Class for specifying options for listing sinks, monitored resources and monitored resource
descriptors.
|
static class |
Logging.SortingField
Fields according to which log entries can be sorted.
|
static class |
Logging.SortingOrder
Sorting orders available when listing log entries.
|
static class |
Logging.WriteOption
Class for specifying options for writing log entries.
|
Modifier and Type | Method and Description |
---|---|
Metric |
create(MetricInfo metric)
Creates a new metric.
|
Sink |
create(SinkInfo sink)
Creates a new sink.
|
com.google.api.core.ApiFuture<Metric> |
createAsync(MetricInfo metric)
Sends a request for creating a metric.
|
com.google.api.core.ApiFuture<Sink> |
createAsync(SinkInfo sink)
Sends a request for creating a sink.
|
boolean |
deleteLog(String log)
Deletes a log and all its log entries.
|
com.google.api.core.ApiFuture<Boolean> |
deleteLogAsync(String log)
Sends a request for deleting a log and all its log entries.
|
boolean |
deleteMetric(String metric)
Deletes the requested metric.
|
com.google.api.core.ApiFuture<Boolean> |
deleteMetricAsync(String metric)
Sends a request for deleting a metric.
|
boolean |
deleteSink(String sink)
Deletes the requested sink.
|
com.google.api.core.ApiFuture<Boolean> |
deleteSinkAsync(String sink)
Sends a request for deleting a sink.
|
void |
flush()
Flushes any pending asynchronous logging writes.
|
Severity |
getFlushSeverity() |
Metric |
getMetric(String metric)
Returns the requested metric or
null if not found. |
com.google.api.core.ApiFuture<Metric> |
getMetricAsync(String metric)
Sends a request for getting a metric.
|
Sink |
getSink(String sink)
Returns the requested sink or
null if not found. |
com.google.api.core.ApiFuture<Sink> |
getSinkAsync(String sink)
Sends a request for getting a sink.
|
Synchronicity |
getWriteSynchronicity() |
com.google.api.gax.paging.Page<LogEntry> |
listLogEntries(Logging.EntryListOption... options)
Lists log entries.
|
com.google.api.core.ApiFuture<com.google.api.gax.paging.AsyncPage<LogEntry>> |
listLogEntriesAsync(Logging.EntryListOption... options)
Sends a request for listing log entries.
|
com.google.api.gax.paging.Page<Metric> |
listMetrics(Logging.ListOption... options)
Lists the metrics.
|
com.google.api.core.ApiFuture<com.google.api.gax.paging.AsyncPage<Metric>> |
listMetricsAsync(Logging.ListOption... options)
Sends a request for listing metrics.
|
com.google.api.gax.paging.Page<MonitoredResourceDescriptor> |
listMonitoredResourceDescriptors(Logging.ListOption... options)
Lists the monitored resource descriptors used by Stackdriver Logging.
|
com.google.api.core.ApiFuture<com.google.api.gax.paging.AsyncPage<MonitoredResourceDescriptor>> |
listMonitoredResourceDescriptorsAsync(Logging.ListOption... options)
Sends a request for listing monitored resource descriptors used by Stackdriver Logging.
|
com.google.api.gax.paging.Page<Sink> |
listSinks(Logging.ListOption... options)
Lists the sinks.
|
com.google.api.core.ApiFuture<com.google.api.gax.paging.AsyncPage<Sink>> |
listSinksAsync(Logging.ListOption... options)
Sends a request for listing sinks.
|
void |
setFlushSeverity(Severity flushSeverity)
Sets flush severity for asynchronous logging writes.
|
void |
setWriteSynchronicity(Synchronicity synchronicity) |
Metric |
update(MetricInfo metric)
Updates a metric or creates one if it does not exist.
|
Sink |
update(SinkInfo sink)
Updates a sink or creates one if it does not exist.
|
com.google.api.core.ApiFuture<Metric> |
updateAsync(MetricInfo metric)
Sends a request for updating a metric (or creating it, if it does not exist).
|
com.google.api.core.ApiFuture<Sink> |
updateAsync(SinkInfo sink)
Sends a request for updating a sink (or creating it, if it does not exist).
|
void |
write(Iterable<LogEntry> logEntries,
Logging.WriteOption... options)
Sends a request to log entries to Stackdriver Logging.
|
close
getOptions
void setWriteSynchronicity(Synchronicity synchronicity)
Synchronicity getWriteSynchronicity()
void setFlushSeverity(Severity flushSeverity)
Enabling this can cause the leaking and hanging threads, see BUG(2796) BUG(3880). However
you can explicitly call flush()
.
TODO: Enable this by default once functionality to trigger rpc is available in generated code.
Severity getFlushSeverity()
Sink create(SinkInfo sink)
Example of creating a sink to export logs to a BigQuery dataset (in the ServiceOptions.getProjectId()
project).
String sinkName = "my_sink_name";
String datasetName = "my_dataset";
SinkInfo sinkInfo = SinkInfo.of(sinkName, DatasetDestination.of(datasetName));
Sink sink = logging.create(sinkInfo);
LoggingException
- upon failurecom.google.api.core.ApiFuture<Sink> createAsync(SinkInfo sink)
ApiFuture
object to consume
the result. Future.get()
returns the created sink.
Example of asynchronously creating a sink to export logs to a BigQuery dataset (in the
ServiceOptions.getProjectId()
project).
String sinkName = "my_sink_name";
String datasetName = "my_dataset";
SinkInfo sinkInfo = SinkInfo.of(sinkName, DatasetDestination.of(datasetName));
ApiFuture<Sink> future = logging.createAsync(sinkInfo);
// ...
Sink sink = future.get();
Sink update(SinkInfo sink)
Example of updating a sink.
String sinkName = "my_sink_name";
String datasetName = "my_dataset";
SinkInfo sinkInfo = SinkInfo.newBuilder(sinkName, DatasetDestination.of(datasetName))
.setVersionFormat(SinkInfo.VersionFormat.V2)
.setFilter("severity>=ERROR")
.build();
Sink sink = logging.update(sinkInfo);
LoggingException
- upon failurecom.google.api.core.ApiFuture<Sink> updateAsync(SinkInfo sink)
ApiFuture
object to consume the result. Future.get()
returns the
updated/created sink or null
if not found.
Example of asynchronously updating a sink.
String sinkName = "my_sink_name";
String datasetName = "my_dataset";
SinkInfo sinkInfo = SinkInfo.newBuilder(sinkName, DatasetDestination.of(datasetName))
.setVersionFormat(SinkInfo.VersionFormat.V2)
.setFilter("severity>=ERROR")
.build();
ApiFuture<Sink> future = logging.updateAsync(sinkInfo);
// ...
Sink sink = future.get();
Sink getSink(String sink)
null
if not found.
Example of getting a sink.
String sinkName = "my_sink_name";
Sink sink = logging.getSink(sinkName);
if (sink == null) {
// sink was not found
}
LoggingException
- upon failurecom.google.api.core.ApiFuture<Sink> getSinkAsync(String sink)
ApiFuture
object to consume
the result. Future.get()
returns the requested sink or null
if not found.
Example of asynchronously getting a sink.
String sinkName = "my_sink_name";
ApiFuture<Sink> future = logging.getSinkAsync(sinkName);
// ...
Sink sink = future.get();
if (sink == null) {
// sink was not found
}
LoggingException
- upon failurecom.google.api.gax.paging.Page<Sink> listSinks(Logging.ListOption... options)
Page
object that can be used to consume
paginated results. Use Logging.ListOption
to specify the page size or the page token from which
to start listing sinks.
Example of listing sinks, specifying the page size.
Page<Sink> sinks = logging.listSinks(ListOption.pageSize(100));
Iterator<Sink> sinkIterator = sinks.iterateAll();
while (sinkIterator.hasNext()) {
Sink sink = sinkIterator.next();
// do something with the sink
}
LoggingException
- upon failurecom.google.api.core.ApiFuture<com.google.api.gax.paging.AsyncPage<Sink>> listSinksAsync(Logging.ListOption... options)
ApiFuture
object to consume
the result. Future.get()
returns an AsyncPage
object that can be used to
asynchronously handle paginated results. Use Logging.ListOption
to specify the page size or the
page token from which to start listing sinks.
Example of asynchronously listing sinks, specifying the page size.
ApiFuture<AsyncPage<Sink>> future = logging.listSinksAsync(ListOption.pageSize(100));
// ...
AsyncPage<Sink> sinks = future.get();
Iterator<Sink> sinkIterator = sinks.iterateAll();
while (sinkIterator.hasNext()) {
Sink sink = sinkIterator.next();
// do something with the sink
}
boolean deleteSink(String sink)
Example of deleting a sink.
String sinkName = "my_sink_name";
boolean deleted = logging.deleteSink(sinkName);
if (deleted) {
// the sink was deleted
} else {
// the sink was not found
}
true
if the sink was deleted, false
if it was not foundcom.google.api.core.ApiFuture<Boolean> deleteSinkAsync(String sink)
ApiFuture
object to consume
the result. Future.get()
returns true
if the sink was deleted, false
if it was not found.
Example of asynchronously deleting a sink.
String sinkName = "my_sink_name";
ApiFuture<Boolean> future = logging.deleteSinkAsync(sinkName);
// ...
boolean deleted = future.get();
if (deleted) {
// the sink was deleted
} else {
// the sink was not found
}
boolean deleteLog(String log)
Example of deleting a log.
String logName = "my_log_name";
boolean deleted = logging.deleteLog(logName);
if (deleted) {
// the log was deleted
} else {
// the log was not found
}
true
if the log was deleted, false
if it was not foundcom.google.api.core.ApiFuture<Boolean> deleteLogAsync(String log)
ApiFuture
object to consume the result. Future.get()
returns true
if the
log was deleted, false
if it was not found.
Example of asynchronously deleting a log.
String logName = "my_log_name";
ApiFuture<Boolean> future = logging.deleteLogAsync(logName);
// ...
boolean deleted = future.get();
if (deleted) {
// the log was deleted
} else {
// the log was not found
}
com.google.api.gax.paging.Page<MonitoredResourceDescriptor> listMonitoredResourceDescriptors(Logging.ListOption... options)
Page
object that can be used to consume paginated results. Use Logging.ListOption
to
specify the page size or the page token from which to start listing resource descriptors.
Example of listing monitored resource descriptors, specifying the page size.
Page<MonitoredResourceDescriptor> descriptors =
logging.listMonitoredResourceDescriptors(ListOption.pageSize(100));
Iterator<MonitoredResourceDescriptor> descriptorIterator = descriptors.iterateAll();
while (descriptorIterator.hasNext()) {
MonitoredResourceDescriptor descriptor = descriptorIterator.next();
// do something with the descriptor
}
LoggingException
- upon failurecom.google.api.core.ApiFuture<com.google.api.gax.paging.AsyncPage<MonitoredResourceDescriptor>> listMonitoredResourceDescriptorsAsync(Logging.ListOption... options)
ApiFuture
object to consume the result. Future.get()
returns an AsyncPage
object that can be used to asynchronously handle paginated
results. Use Logging.ListOption
to specify the page size or the page token from which to start
listing resource descriptors.
Example of asynchronously listing monitored resource descriptors, specifying the page size.
ApiFuture<AsyncPage<MonitoredResourceDescriptor>> future =
logging.listMonitoredResourceDescriptorsAsync(ListOption.pageSize(100));
// ...
AsyncPage<MonitoredResourceDescriptor> descriptors = future.get();
Iterator<MonitoredResourceDescriptor> descriptorIterator = descriptors.iterateAll();
while (descriptorIterator.hasNext()) {
MonitoredResourceDescriptor descriptor = descriptorIterator.next();
// do something with the descriptor
}
Metric create(MetricInfo metric)
Example of creating a metric for logs with severity higher or equal to ERROR.
String metricName = "my_metric_name";
MetricInfo metricInfo = MetricInfo.of(metricName, "severity>=ERROR");
Metric metric = logging.create(metricInfo);
LoggingException
- upon failurecom.google.api.core.ApiFuture<Metric> createAsync(MetricInfo metric)
ApiFuture
object to
consume the result. Future.get()
returns the created metric.
Example of asynchronously creating a metric for logs with severity higher or equal to ERROR.
String metricName = "my_metric_name";
MetricInfo metricInfo = MetricInfo.of(metricName, "severity>=ERROR");
ApiFuture<Metric> future = logging.createAsync(metricInfo);
// ...
Metric metric = future.get();
Metric update(MetricInfo metric)
Example of updating a metric.
String metricName = "my_metric_name";
MetricInfo metricInfo = MetricInfo.newBuilder(metricName, "severity>=ERROR")
.setDescription("new description")
.build();
Metric metric = logging.update(metricInfo);
LoggingException
- upon failurecom.google.api.core.ApiFuture<Metric> updateAsync(MetricInfo metric)
ApiFuture
object to consume the result. Future.get()
returns the
updated/created metric or null
if not found.
Example of asynchronously updating a metric.
String metricName = "my_metric_name";
MetricInfo metricInfo = MetricInfo.newBuilder(metricName, "severity>=ERROR")
.setDescription("new description")
.build();
ApiFuture<Metric> future = logging.updateAsync(metricInfo);
// ...
Metric metric = future.get();
Metric getMetric(String metric)
null
if not found.
Example of getting a metric.
String metricName = "my_metric_name";
Metric metric = logging.getMetric(metricName);
if (metric == null) {
// metric was not found
}
LoggingException
- upon failurecom.google.api.core.ApiFuture<Metric> getMetricAsync(String metric)
ApiFuture
object to consume
the result. Future.get()
returns the requested metric or null
if not found.
Example of asynchronously getting a metric.
String metricName = "my_metric_name";
ApiFuture<Metric> future = logging.getMetricAsync(metricName);
// ...
Metric metric = future.get();
if (metric == null) {
// metric was not found
}
LoggingException
- upon failurecom.google.api.gax.paging.Page<Metric> listMetrics(Logging.ListOption... options)
Page
object that can be used to consume
paginated results. Use Logging.ListOption
to specify the page size or the page token from which
to start listing metrics.
Example of listing metrics, specifying the page size.
Page<Metric> metrics = logging.listMetrics(ListOption.pageSize(100));
Iterator<Metric> metricIterator = metrics.iterateAll();
while (metricIterator.hasNext()) {
Metric metric = metricIterator.next();
// do something with the metric
}
LoggingException
- upon failurecom.google.api.core.ApiFuture<com.google.api.gax.paging.AsyncPage<Metric>> listMetricsAsync(Logging.ListOption... options)
ApiFuture
object to consume
the result. Future.get()
returns an AsyncPage
object that can be used to
asynchronously handle paginated results. Use Logging.ListOption
to specify the page size or the
page token from which to start listing metrics.
Example of asynchronously listing metrics, specifying the page size.
ApiFuture<AsyncPage<Metric>> future = logging.listMetricsAsync(ListOption.pageSize(100));
// ...
AsyncPage<Metric> metrics = future.get();
Iterator<Metric> metricIterator = metrics.iterateAll();
while (metricIterator.hasNext()) {
Metric metric = metricIterator.next();
// do something with the metric
}
boolean deleteMetric(String metric)
Example of deleting a metric.
String metricName = "my_metric_name";
boolean deleted = logging.deleteMetric(metricName);
if (deleted) {
// the metric was deleted
} else {
// the metric was not found
}
true
if the metric was deleted, false
if it was not foundcom.google.api.core.ApiFuture<Boolean> deleteMetricAsync(String metric)
ApiFuture
object to
consume the result. Future.get()
returns true
if the metric was deleted,
false
if it was not found.
Example of asynchronously deleting a metric.
String metricName = "my_metric_name";
ApiFuture<Boolean> future = logging.deleteMetricAsync(metricName);
// ...
boolean deleted = future.get();
if (deleted) {
// the metric was deleted
} else {
// the metric was not found
}
void flush()
BatchingSettings
,
Logs are also flushed if enabled, at or above flush severity, see setFlushSeverity(com.google.cloud.logging.Severity)
.
Logging frameworks require support for an explicit flush. See usage in the java.util.logging
handlerLoggingHandler
.void write(Iterable<LogEntry> logEntries, Logging.WriteOption... options)
Logging.WriteOption.logName(String)
to provide a log name for those entries that do not specify one. Use Logging.WriteOption.resource(MonitoredResource)
to provide a monitored resource for those entries that
do not specify one. Use Logging.WriteOption.labels(Map)
to provide some labels to be added to
every entry in logEntries
.
Example of writing log entries and providing a default log name and monitored resource.
String logName = "my_log_name";
List<LogEntry> entries = new ArrayList<>();
entries.add(LogEntry.of(StringPayload.of("Entry payload")));
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("key", "value");
entries.add(LogEntry.of(JsonPayload.of(jsonMap)));
logging.write(
entries,
WriteOption.logName(logName),
WriteOption.resource(MonitoredResource.newBuilder("global").build()));
com.google.api.gax.paging.Page<LogEntry> listLogEntries(Logging.EntryListOption... options)
Page
object that can be used to consume
paginated results. Use Logging.EntryListOption.pageSize(int)
to specify the page size. Use
Logging.EntryListOption.pageToken(String)
to specify the page token from which to start listing
entries. Use EntryListOption#sortOrder(SortingField, SortingOrder)
to sort log entries
according to your preferred order (default is most-recent last). Use Logging.EntryListOption.filter(String)
to filter listed log entries.
Example of listing log entries for a specific log.
String filter = "logName=projects/my_project_id/logs/my_log_name";
Page<LogEntry> entries = logging.listLogEntries(EntryListOption.filter(filter));
Iterator<LogEntry> entryIterator = entries.iterateAll();
while (entryIterator.hasNext()) {
LogEntry entry = entryIterator.next();
// do something with the entry
}
LoggingException
- upon failurecom.google.api.core.ApiFuture<com.google.api.gax.paging.AsyncPage<LogEntry>> listLogEntriesAsync(Logging.EntryListOption... options)
ApiFuture
object to
consume the result. Future.get()
returns an AsyncPage
object that can be
used to asynchronously handle paginated results. Use Logging.EntryListOption.pageSize(int)
to
specify the page size. Use Logging.EntryListOption.pageToken(String)
to specify the page token
from which to start listing entries. Use EntryListOption#sortOrder(SortingField,
SortingOrder)
to sort log entries according to your preferred order (default is most-recent
last). Use Logging.EntryListOption.filter(String)
to filter listed log entries.
Example of asynchronously listing log entries for a specific log.
String filter = "logName=projects/my_project_id/logs/my_log_name";
ApiFuture<AsyncPage<LogEntry>> future =
logging.listLogEntriesAsync(EntryListOption.filter(filter));
// ...
AsyncPage<LogEntry> entries = future.get();
Iterator<LogEntry> entryIterator = entries.iterateAll();
while (entryIterator.hasNext()) {
LogEntry entry = entryIterator.next();
// do something with the entry
}
LoggingException
- upon failureCopyright © 2019 Google LLC. All rights reserved.