use of com.google.cloud.logging.LoggingOptions in project google-cloud-java by GoogleCloudPlatform.
the class RemoteLoggingHelper method create.
/**
* Creates a {@code RemoteLoggingHelper} object for the given project id and JSON key input
* stream.
*
* @param projectId id of the project to be used for running the tests
* @param keyStream input stream for a JSON key
* @return A {@code RemoteLoggingHelper} object for the provided options
* @throws com.google.cloud.logging.testing.RemoteLoggingHelper.LoggingHelperException if
* {@code keyStream} is not a valid JSON key stream
*/
public static RemoteLoggingHelper create(String projectId, InputStream keyStream) throws LoggingHelperException {
try {
GrpcTransportOptions transportOptions = LoggingOptions.getDefaultGrpcTransportOptions();
LoggingOptions storageOptions = LoggingOptions.newBuilder().setCredentials(ServiceAccountCredentials.fromStream(keyStream)).setProjectId(projectId).setRetrySettings(retrySettings()).setTransportOptions(transportOptions).build();
return new RemoteLoggingHelper(storageOptions);
} catch (IOException ex) {
if (log.isLoggable(Level.WARNING)) {
log.log(Level.WARNING, ex.getMessage());
}
throw LoggingHelperException.translate(ex);
}
}
use of com.google.cloud.logging.LoggingOptions in project google-cloud-java by GoogleCloudPlatform.
the class RemoteLoggingHelper method create.
/**
* Creates a {@code RemoteLoggingHelper} object using default project id and authentication
* credentials.
*/
public static RemoteLoggingHelper create() throws LoggingHelperException {
GrpcTransportOptions transportOptions = LoggingOptions.getDefaultGrpcTransportOptions();
LoggingOptions loggingOptions = LoggingOptions.newBuilder().setRetrySettings(retrySettings()).setTransportOptions(transportOptions).build();
return new RemoteLoggingHelper(loggingOptions);
}
use of com.google.cloud.logging.LoggingOptions in project google-cloud-java by GoogleCloudPlatform.
the class WriteAndListLogEntries method main.
public static void main(String... args) throws Exception {
// Create a service object
// Credentials are inferred from the environment
LoggingOptions options = LoggingOptions.getDefaultInstance();
try (Logging logging = options.getService()) {
// Create a log entry
LogEntry firstEntry = LogEntry.newBuilder(StringPayload.of("message")).setLogName("test-log").setResource(MonitoredResource.newBuilder("global").addLabel("project_id", options.getProjectId()).build()).build();
logging.write(Collections.singleton(firstEntry));
// List log entries
Page<LogEntry> entries = logging.listLogEntries(EntryListOption.filter("logName=projects/" + options.getProjectId() + "/logs/test-log"));
for (LogEntry logEntry : entries.iterateAll()) {
System.out.println(logEntry);
}
}
}
use of com.google.cloud.logging.LoggingOptions in project jetty-runtime by GoogleCloudPlatform.
the class LoggingIntegrationTest method testLogging.
@Test
@RemoteOnly
public void testLogging() throws Exception {
// Create unique ID to relate request with log entries
String id = Long.toHexString(System.nanoTime());
// Hit the DumpServlet that will log the request path
URI target = getUri().resolve("/dump/info/" + id);
HttpURLConnection http = HttpUrlUtil.openTo(target);
assertThat(http.getResponseCode(), is(200));
String responseBody = HttpUrlUtil.getResponseBody(http);
List<String> lines = new BufferedReader(new StringReader(responseBody)).lines().collect(Collectors.toList());
assertThat(lines.stream().filter(s -> s.startsWith("requestURI=")).findFirst().get(), containsString(id));
String traceId = lines.stream().filter(s -> s.startsWith("X-Cloud-Trace-Context: ")).findFirst().get().split("[ /]")[1];
assertThat(traceId, Matchers.notNullValue());
LoggingOptions options = LoggingOptions.getDefaultInstance();
String filter = "resource.type=gae_app AND resource.labels.module_id=smoke" + " AND textPayload:" + id;
int expected = 2;
try (Logging logging = options.getService()) {
Page<LogEntry> entries = logging.listLogEntries(EntryListOption.filter(filter));
for (LogEntry entry : entries.iterateAll()) {
if (entry.getSeverity() == Severity.INFO) {
assertThat(entry.getLogName(), is("gae_app.log"));
assertThat(entry.getResource().getType(), is("gae_app"));
assertThat(entry.getResource().getLabels().get("module_id"), is("smoke"));
assertThat(entry.getResource().getLabels().get("zone"), Matchers.notNullValue());
assertThat(entry.getLabels().get("appengine.googleapis.com/trace_id"), is(traceId));
assertThat(entry.getLabels().get("appengine.googleapis.com/instance_name"), Matchers.notNullValue());
if (entry.getPayload().toString().contains("JUL.info:/dump/info/")) {
expected--;
assertThat(entry.getPayload().toString(), Matchers.containsString(id));
}
if (entry.getPayload().toString().contains("ServletContext.log:/dump/info")) {
expected--;
assertThat(entry.getPayload().toString(), Matchers.containsString(id));
}
}
}
assertThat(expected, is(0));
}
}
Aggregations