Skip navigation links

Package com.google.cloud.logging

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

See: Description

Package com.google.cloud.logging Description

A client for Stackdriver 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 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
Skip navigation links

Copyright © 2019 Google LLC. All rights reserved.