google-cloud-logging overview (3.14.9)

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.

LoggingOptionsoptions=LoggingOptions.getDefaultInstance();try(Logginglogging=options.getService()){LogEntryfirstEntry=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 ajava.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 thelogging.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.

Loggerlogger=Logger.getLogger(AddLoggingHandler.class.getName());LoggingHandler.addHandler(logger,newLoggingHandler());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:

RemoteLoggingHelperhelper=RemoteLoggingHelper.create();Logginglogging=helper.getOptions().getService();

Format resource names to avoid name clashes:

StringmetricName=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_librarytry(LoggingClientloggingClient=LoggingClient.create()){LogNamelogName=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_librarytry(ConfigClientconfigClient=ConfigClient.create()){GetBucketRequestrequest=GetBucketRequest.newBuilder().setName(LogBucketName.ofProjectLocationBucketName("[PROJECT]","[LOCATION]","[BUCKET]").toString()).build();LogBucketresponse=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_librarytry(MetricsClientmetricsClient=MetricsClient.create()){LogMetricNamemetricName=LogMetricName.of("[PROJECT]","[METRIC]");LogMetricresponse=metricsClient.getLogMetric(metricName);}

com.google.cloud.logging.v2.stub

com.google.logging.v2

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-10-30 UTC.