use of com.google.cloud.logging.LogEntry in project google-cloud-java by GoogleCloudPlatform.
the class LoggingAppenderTest method testDefaultWriteOptionsHasExpectedDefaults.
@Test
public void testDefaultWriteOptionsHasExpectedDefaults() {
logging.setFlushSeverity(Severity.ERROR);
Capture<WriteOption> logNameArg = Capture.newInstance();
Capture<WriteOption> resourceArg = Capture.newInstance();
logging.write((Iterable<LogEntry>) anyObject(), capture(logNameArg), capture(resourceArg));
expectLastCall().once();
replay(logging);
loggingAppender.start();
Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0);
LoggingEvent loggingEvent = createLoggingEvent(Level.ERROR, timestamp.getSeconds());
loggingAppender.doAppend(loggingEvent);
Assert.assertTrue(logNameArg.getValue().equals(defaultWriteOptions[0]));
Assert.assertTrue(resourceArg.getValue().equals(defaultWriteOptions[1]));
}
use of com.google.cloud.logging.LogEntry in project google-cloud-java by GoogleCloudPlatform.
the class LoggingAppenderTest method testFilterLogsOnlyLogsAtOrAboveLogLevel.
@Test
public void testFilterLogsOnlyLogsAtOrAboveLogLevel() {
LogEntry logEntry = LogEntry.newBuilder(StringPayload.of("this is a test")).setTimestamp(100000L).setSeverity(Severity.ERROR).setLabels(new ImmutableMap.Builder<String, String>().put("levelName", "ERROR").put("levelValue", String.valueOf(40000L)).build()).build();
logging.setFlushSeverity(Severity.ERROR);
Capture<Iterable<LogEntry>> capturedArgument = Capture.newInstance();
logging.write(capture(capturedArgument), (WriteOption) anyObject(), (WriteOption) anyObject());
expectLastCall().once();
replay(logging);
Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0);
LoggingEvent loggingEvent1 = createLoggingEvent(Level.INFO, timestamp.getSeconds());
ThresholdFilter thresholdFilter = new ThresholdFilter();
thresholdFilter.setLevel("ERROR");
thresholdFilter.start();
loggingAppender.addFilter(thresholdFilter);
loggingAppender.start();
// info event does not get logged
loggingAppender.doAppend(loggingEvent1);
LoggingEvent loggingEvent2 = createLoggingEvent(Level.ERROR, timestamp.getSeconds());
// error event gets logged
loggingAppender.doAppend(loggingEvent2);
verify(logging);
Assert.assertTrue(capturedArgument.getValue().iterator().hasNext());
Assert.assertTrue(logEntry.equals(capturedArgument.getValue().iterator().next()));
}
use of com.google.cloud.logging.LogEntry in project google-cloud-java by GoogleCloudPlatform.
the class LoggingAppenderTest method testEnhancersAddCorrectLabelsToLogEntries.
@Test
public void testEnhancersAddCorrectLabelsToLogEntries() {
LogEntry logEntry = LogEntry.newBuilder(StringPayload.of("this is a test")).setTimestamp(100000L).setSeverity(Severity.WARNING).setLabels(new ImmutableMap.Builder<String, String>().put("levelName", "WARN").put("levelValue", String.valueOf(30000L)).put("test-label-1", "test-value-1").put("test-label-2", "test-value-2").build()).build();
logging.setFlushSeverity(Severity.ERROR);
Capture<Iterable<LogEntry>> capturedArgument = Capture.newInstance();
logging.write(capture(capturedArgument), (WriteOption) anyObject(), (WriteOption) anyObject());
expectLastCall().once();
replay(logging);
loggingAppender.addEnhancer("com.example.enhancers.TestLoggingEnhancer");
loggingAppender.addEnhancer("com.example.enhancers.AnotherTestLoggingEnhancer");
loggingAppender.start();
Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0);
LoggingEvent loggingEvent = createLoggingEvent(Level.WARN, timestamp.getSeconds());
loggingAppender.doAppend(loggingEvent);
verify(logging);
Assert.assertTrue(capturedArgument.getValue().iterator().hasNext());
Assert.assertTrue(logEntry.equals(capturedArgument.getValue().iterator().next()));
}
use of com.google.cloud.logging.LogEntry in project google-cloud-java by GoogleCloudPlatform.
the class LoggingAppenderTest method testFlushLevelConfigUpdatesLoggingFlushSeverity.
@Test
public void testFlushLevelConfigUpdatesLoggingFlushSeverity() {
LogEntry logEntry = LogEntry.newBuilder(StringPayload.of("this is a test")).setTimestamp(100000L).setSeverity(Severity.WARNING).setLabels(new ImmutableMap.Builder<String, String>().put("levelName", "WARN").put("levelValue", String.valueOf(30000L)).build()).build();
logging.setFlushSeverity(Severity.WARNING);
Capture<Iterable<LogEntry>> capturedArgument = Capture.newInstance();
logging.write(capture(capturedArgument), (WriteOption) anyObject(), (WriteOption) anyObject());
replay(logging);
Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0);
LoggingEvent loggingEvent = createLoggingEvent(Level.WARN, timestamp.getSeconds());
// error is the default, updating to warn for test
loggingAppender.setFlushLevel(Level.WARN);
loggingAppender.start();
loggingAppender.doAppend(loggingEvent);
verify(logging);
Assert.assertTrue(capturedArgument.getValue().iterator().hasNext());
Assert.assertTrue(logEntry.equals(capturedArgument.getValue().iterator().next()));
}
use of com.google.cloud.logging.LogEntry 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);
}
}
}
Aggregations