google-cloud-logging overview (3.15.17)

com.google.cloud.logging

A client for Cloud Logging - Real-time log management and analysis.

Here's a simple usage example for using google-cloud from Compute Engine/App Engine Flexible. This example shows how to write and list log entries. For the complete source code see WriteAndListLogEntries.java.


 LoggingOptions options = LoggingOptions.getDefaultInstance();
 try(Logging logging = options.getService()) {

   LogEntry firstEntry = LogEntry.newBuilder(StringPayload.of("message"))
       .setLogName("test-log")
       .setResource(MonitoredResource.builder("global")
           .addLabel("project_id", options.getProjectId())
           .build())
       .build();
   logging.write(Collections.singleton(firstEntry));

   Page<LogEntry> entries = logging.listLogEntries(
   EntryListOption.filter("logName=projects/" + options.getProjectId() + "/logs/test-log"));
   Iterator<LogEntry> entryIterator = entries.iterateAll();
   while (entryIterator.hasNext()) {
     System.out.println(entryIterator.next());
   }
 }
 

This second example shows how to use a java.util.logging.Logger to write log entries to Cloud Logging. The snippet installs a Cloud Logging handler using LoggingHandler.addHandler(Logger, LoggingHandler). Notice that this could also be done through the logging.properties file, adding the following line:

 com.google.cloud.examples.logging.snippets.AddLoggingHandler.handlers=com.google.cloud.logging.LoggingHandler
 

For the complete source code see AddLoggingHandler.java.


 Logger logger = Logger.getLogger(AddLoggingHandler.class.getName());
 LoggingHandler.addHandler(logger, new LoggingHandler());
 logger.warning("test warning");
 

See Also: Cloud Logging

com.google.cloud.logging.spi

com.google.cloud.logging.spi.v2

com.google.cloud.logging.testing

A testing helper for Cloud Logging.

A simple usage example:

Before the test:


 RemoteLoggingHelper helper = RemoteLoggingHelper.create();
 Logging logging = helper.getOptions().getService();
 

Format resource names to avoid name clashes:


 String metricName = RemoteLoggingHelper.formatForTest("test-metric");
 

See Also: Google Cloud Java tools for testing

com.google.cloud.logging.v2

A client to Cloud Logging API

The interfaces provided are listed below, along with usage samples.

LoggingClient

Service Description: Service for ingesting and querying logs.

Sample for LoggingClient:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 try (LoggingClient loggingClient = LoggingClient.create()) {
   LogName logName = LogName.ofProjectLogName("[PROJECT]", "[LOG]");
   loggingClient.deleteLog(logName);
 }
 

ConfigClient

Service Description: Service for configuring sinks used to route log entries.

Sample for ConfigClient:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 try (ConfigClient configClient = ConfigClient.create()) {
   GetBucketRequest request =
       GetBucketRequest.newBuilder()
           .setName(
               LogBucketName.ofProjectLocationBucketName("[PROJECT]", "[LOCATION]", "[BUCKET]")
                   .toString())
           .build();
   LogBucket response = configClient.getBucket(request);
 }
 

MetricsClient

Service Description: Service for configuring logs-based metrics.

Sample for MetricsClient:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 try (MetricsClient metricsClient = MetricsClient.create()) {
   LogMetricName metricName = LogMetricName.of("[PROJECT]", "[METRIC]");
   LogMetric response = metricsClient.getLogMetric(metricName);
 }
 

com.google.cloud.logging.v2.stub

com.google.logging.v2