google-cloud-logging overview (3.5.3) Stay organized with collections Save and categorize content based on your preferences.
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));PageThis 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.LoggingHandlerFor the complete source code see AddLoggingHandler.java.
Loggerlogger=Logger.getLogger(AddLoggingHandler.class.getName());LoggingHandler.addHandler(logger,newLoggingHandler());logger.warning("test warning");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");com.google.cloud.logging.v2
The interfaces provided are listed below, along with usage samples.
LoggingClient
Service Description: Service for ingesting and querying logs.
Sample for LoggingClient:
try(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:
try(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:
try(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.