Skip to content

docs(logging): improve write log entry sample#12974

Open
MukundaKatta wants to merge 1 commit intogoogleapis:mainfrom
MukundaKatta:codex/java-logging-write-log-entry
Open

docs(logging): improve write log entry sample#12974
MukundaKatta wants to merge 1 commit intogoogleapis:mainfrom
MukundaKatta:codex/java-logging-write-log-entry

Conversation

@MukundaKatta
Copy link
Copy Markdown

Fixes #11923.

Updates the Java Cloud Logging write log entry sample to demonstrate:

  • textPayload and jsonPayload entries
  • severity, labels, and the global monitored resource
  • writing one log entry and writing a batch of entries

The accompanying sample test now asserts the constructed entry metadata and payload types before exercising the sample.

Verification:

  • git diff --check

Not run locally:

  • mvn -pl java-logging/samples/snippets -Dtest=LoggingIT#testWriteLogEntrySample test (mvn is not installed in this environment)

@MukundaKatta MukundaKatta requested a review from a team as a code owner April 30, 2026 22:26
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the WriteLogEntry sample to demonstrate both single and batch log writing by introducing a createLogEntries helper method. It also enhances the integration tests to validate the specific attributes of the generated log entries. Review feedback suggests avoiding duplicate log writes by using a sublist for the batch operation and renaming a variable that shadows the JsonPayload class to improve clarity.

Comment on lines +44 to +47
logging.write(Collections.singleton(entries.get(0)));

// Writes a batch of text and structured log entries.
logging.write(entries);
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()));

Comment on lines +67 to +71
Map<String, Object> jsonPayload =
ImmutableMap.of(
"message", "Structured log entry written from Java.", "component", "sample");
LogEntry structEntry =
LogEntry.newBuilder(JsonPayload.of(jsonPayload))
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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java-logging] Improve the Write logEntry sample

1 participant