Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import com.google.cloud.logging.Logging;
import com.google.cloud.logging.LoggingOptions;
import com.google.cloud.logging.Payload.JsonPayload;
import com.google.cloud.logging.Payload.StringPayload;
import com.google.cloud.logging.Severity;
import com.google.common.collect.ImmutableMap;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class WriteLogEntry {
Expand All @@ -35,23 +38,44 @@ public static void main(String[] args) throws Exception {

// Instantiates a client
try (Logging logging = LoggingOptions.getDefaultInstance().getService()) {
Map<String, String> payload =
ImmutableMap.of(
"name", "King Arthur", "quest", "Find the Holy Grail", "favorite_color", "Blue");
LogEntry entry =
LogEntry.newBuilder(JsonPayload.of(payload))
.setSeverity(Severity.INFO)
.setLogName(logName)
.setResource(MonitoredResource.newBuilder("global").build())
.build();

// Writes the log entry asynchronously
logging.write(Collections.singleton(entry));
List<LogEntry> entries = createLogEntries(logName);

// Writes one text log entry.
logging.write(Collections.singleton(entries.get(0)));

// Writes a batch of text and structured log entries.
logging.write(entries);
Comment on lines +44 to +47
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation writes the first entry (entries.get(0)) twice: once individually at line 44 and again as part of the batch at line 47. This will result in duplicate log entries in Cloud Logging. While this demonstrates both APIs, it might be confusing for users. Consider using distinct entries or a sublist for the batch write to avoid duplication.

Suggested change
logging.write(Collections.singleton(entries.get(0)));
// Writes a batch of text and structured log entries.
logging.write(entries);
// Writes one text log entry.
logging.write(Collections.singleton(entries.get(0)));
// Writes a batch of structured log entries.
logging.write(entries.subList(1, entries.size()));


// Optional - flush any pending log entries just before Logging is closed
logging.flush();
}
System.out.printf("Wrote to %s\n", logName);
}

static List<LogEntry> createLogEntries(String logName) {
MonitoredResource resource = MonitoredResource.newBuilder("global").build();
Map<String, String> labels = ImmutableMap.of("sample", "write-log-entry");

LogEntry textEntry =
LogEntry.newBuilder(StringPayload.of("Text log entry written from Java."))
.setSeverity(Severity.INFO)
.setLogName(logName)
.setResource(resource)
.setLabels(labels)
.build();

Map<String, Object> jsonPayload =
ImmutableMap.of(
"message", "Structured log entry written from Java.", "component", "sample");
LogEntry structEntry =
LogEntry.newBuilder(JsonPayload.of(jsonPayload))
Comment on lines +67 to +71
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The local variable jsonPayload shadows the JsonPayload class name (differing only by case), which is imported and used in the same scope (line 71). This can lead to confusion and is generally discouraged in Java. Consider renaming the variable to payload or payloadMap.

Suggested change
Map<String, Object> jsonPayload =
ImmutableMap.of(
"message", "Structured log entry written from Java.", "component", "sample");
LogEntry structEntry =
LogEntry.newBuilder(JsonPayload.of(jsonPayload))
Map<String, Object> payload =
ImmutableMap.of(
"message", "Structured log entry written from Java.", "component", "sample");
LogEntry structEntry =
LogEntry.newBuilder(JsonPayload.of(payload))

.setSeverity(Severity.WARNING)
.setLogName(logName)
.setResource(resource)
.setLabels(labels)
.build();

return Arrays.asList(textEntry, structEntry);
}
}
// [END logging_write_log_entry]
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import com.google.cloud.logging.LogEntry;
import com.google.cloud.logging.Logging;
import com.google.cloud.logging.LoggingOptions;
import com.google.cloud.logging.Payload.JsonPayload;
import com.google.cloud.logging.Payload.StringPayload;
import com.google.cloud.logging.Severity;
import com.google.cloud.logging.Synchronicity;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Collections;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
Expand Down Expand Up @@ -152,6 +155,27 @@ public void testWriteLogHttpRequestSample() throws Exception {

@Test(timeout = 60000)
public void testWriteLogEntrySample() throws Exception {
List<LogEntry> entries = WriteLogEntry.createLogEntries(TEST_LOG);
assertThat(entries).hasSize(2);

LogEntry textEntry = entries.get(0);
assertThat(textEntry.getLogName()).isEqualTo(TEST_LOG);
assertThat(textEntry.getResource().getType()).isEqualTo("global");
assertThat(textEntry.getSeverity()).isEqualTo(Severity.INFO);
assertThat(textEntry.getLabels()).containsEntry("sample", "write-log-entry");
assertThat(textEntry.getPayload()).isInstanceOf(StringPayload.class);
assertThat(((StringPayload) textEntry.getPayload()).getData())
.isEqualTo("Text log entry written from Java.");

LogEntry structEntry = entries.get(1);
assertThat(structEntry.getLogName()).isEqualTo(TEST_LOG);
assertThat(structEntry.getResource().getType()).isEqualTo("global");
assertThat(structEntry.getSeverity()).isEqualTo(Severity.WARNING);
assertThat(structEntry.getLabels()).containsEntry("sample", "write-log-entry");
assertThat(structEntry.getPayload()).isInstanceOf(JsonPayload.class);
assertThat(((JsonPayload) structEntry.getPayload()).getData())
.containsEntry("message", "Structured log entry written from Java.");

WriteLogEntry.main(new String[] {TEST_LOG});
String got = bout.toString();
assertThat(got).contains(String.format("Wrote to %s", TEST_LOG));
Expand Down
Loading