Skip to content

Update function to handle multi-line commands for timestamps#5241

Open
agrawalkhushi18 wants to merge 1 commit intoGoogleCloudPlatform:developfrom
agrawalkhushi18:logging-update
Open

Update function to handle multi-line commands for timestamps#5241
agrawalkhushi18 wants to merge 1 commit intoGoogleCloudPlatform:developfrom
agrawalkhushi18:logging-update

Conversation

@agrawalkhushi18
Copy link
Contributor

This PR replaces timestamping in pkg/shell/writer.go with a character check (' ', '\t', '\n') to prevent injecting timestamps into indented multi-line process logs (like gcloud commands), preserving their original visual layout and readability. Updated
pkg/shell/writer_test.go to assert this new formatting behavior cleanly handles whitespace-prefixed segments without adding extra timestamps.

Submission Checklist

NOTE: Community submissions can take up to 2 weeks to be reviewed.

Please take the following actions before submitting this pull request.

  • Fork your PR branch from the Toolkit "develop" branch (not main)
  • Test all changes with pre-commit in a local branch #
  • Confirm that "make tests" passes all tests
  • Add or modify unit tests to cover code changes
  • Ensure that unit test coverage remains above 80%
  • Update all applicable documentation
  • Follow Cluster Toolkit Contribution guidelines #

@agrawalkhushi18 agrawalkhushi18 requested review from a team and samskillman as code owners February 13, 2026 10:58
@agrawalkhushi18 agrawalkhushi18 added the release-chore To not include into release notes label Feb 13, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @agrawalkhushi18, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines the timestamping behavior within the shell writer to significantly improve the readability of multi-line process logs. By introducing a character-based check, the system now intelligently avoids prepending timestamps to lines that are indented or empty, thereby preserving the original visual structure of complex command outputs, such as those from gcloud. This change directly addresses formatting disruptions caused by indiscriminate timestamp injection.

Highlights

  • Conditional Timestamping: Implemented logic in pkg/shell/writer.go to prevent timestamps from being injected into log lines that start with whitespace (space, tab) or are blank lines. This preserves the visual layout of multi-line commands.
  • Enhanced Test Coverage: Added new test cases in pkg/shell/writer_test.go to specifically verify that indented lines and blank lines are correctly handled and do not receive timestamps.
Changelog
  • pkg/shell/writer.go
    • Modified the writeSegment function to include a conditional check on the first character of a log segment.
    • Added conditions to prevent timestamp injection if the segment begins with a space, tab, or newline character.
  • pkg/shell/writer_test.go
    • Introduced new test cases within TestTimestampWriter to validate the correct behavior for indented lines and blank lines, ensuring timestamps are not added in these specific scenarios.
Activity
  • No activity has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
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 effectively addresses an issue where timestamps were incorrectly injected into indented, multi-line log outputs, which could disrupt formatting. The approach of checking the first character of a line segment for whitespace before adding a timestamp is sound, and the new test cases in pkg/shell/writer_test.go correctly validate this behavior. I have one suggestion to refactor the conditional logic in pkg/shell/writer.go to improve readability and maintainability.

Comment on lines +69 to +77
if len(p) > 0 {
firstChar := p[0]
// Avoid printing timestamp if the segment starts with an indentation or newline
if firstChar != ' ' && firstChar != '\t' && firstChar != '\n' {
ts := time.Now().UTC().Format(time.RFC3339)
coloredTs := logging.TsColor.Sprint(ts)
buf.WriteString(coloredTs + " ")
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For better readability and maintainability, consider using a switch statement to check for leading whitespace characters. This makes the logic clearer than a chain of && conditions and is more easily extensible if you need to handle more character types in the future.

                if len(p) > 0 {
                        switch p[0] {
                        case ' ', '\t', '\n':
                                // Do not add timestamp for indented or empty lines.
                        default:
                                ts := time.Now().UTC().Format(time.RFC3339)
                                coloredTs := logging.TsColor.Sprint(ts)
                                buf.WriteString(coloredTs + " ")
                        }
                }
References
  1. The style guide emphasizes that code should be maintainable and easy to understand (line 6). Using a switch statement improves readability and maintainability over a complex if condition, which aligns with Go best practices mentioned in the style guide (line 27). (link)

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

Labels

release-chore To not include into release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant