Package com.google.cloud.logging

A client to Stackdriver Logging.

See: Description

Package com.google.cloud.logging Description

A client to Stackdriver Logging.

Here's a simple usage example for using gcloud-java 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.defaultInstance();
 try(Logging logging = options.service()) {

   LogEntry firstEntry = LogEntry.builder(StringPayload.of("message"))
       .logName("test-log")
       .resource(MonitoredResource.builder("global")
           .addLabel("project_id", options.projectId())
           .build())
       .build();
   logging.write(Collections.singleton(firstEntry));

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

This second example shows how to use a Logger to write log entries to Stackdriver Logging. The snippet installs a Stackdriver 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:
Stackdriver Logging

Copyright © 2016 Google. All rights reserved.