-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: add color to velero logs #9317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
iTrooz
wants to merge
17
commits into
vmware-tanzu:main
Choose a base branch
from
iTrooz:color
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+222
−6
Draft
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8f26b0a
feat: make log level colored in `velero backup logs`
iTrooz bb975b1
use logfmt for parsing logs
iTrooz 6e56558
colorize level value as well + refactor
iTrooz f15192c
fix typo
iTrooz 54c9625
If NoColor, do not parse logs
iTrooz 0150f06
wait for all logs to be processed before exiting
iTrooz c9ebc42
handle logs processing error
iTrooz 3114ee6
refactor into a different file + add helper function to avoid clutter…
iTrooz fa40bbb
implement for restore logs too
iTrooz 4ca4827
add changelog entry
iTrooz 6b1f384
go mod tidy
iTrooz eeaec82
quote value if necessary
iTrooz 61a5416
update doc
iTrooz 05f9f0f
add copyright
iTrooz 5a42287
do not print extra space after lines
iTrooz 972b28d
add comments
iTrooz 2fb573e
add tests
iTrooz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| add color to velero logs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| Copyright the Velero contributors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package output | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "strings" | ||
| "sync" | ||
| "unicode/utf8" | ||
|
|
||
| "github.com/fatih/color" | ||
| "github.com/go-logfmt/logfmt" | ||
| ) | ||
|
|
||
| func getLevelColor(level string) *color.Color { | ||
| switch level { | ||
| case "info": | ||
| return color.New(color.FgGreen) | ||
| case "warning": | ||
| return color.New(color.FgYellow) | ||
| case "error": | ||
| return color.New(color.FgRed) | ||
| case "debug": | ||
| return color.New(color.FgBlue) | ||
| default: | ||
| return color.New() | ||
| } | ||
| } | ||
|
|
||
| // https://github.com/go-logfmt/logfmt/blob/e5396c6ee35145aead27da56e7921a7656f69624/encode.go#L235 | ||
| func needsQuotedValueRune(r rune) bool { | ||
| return r <= ' ' || r == '=' || r == '"' || r == 0x7f || r == utf8.RuneError | ||
| } | ||
|
|
||
| // Process logs (by adding color) before printing them | ||
| func processAndPrintLogs(r io.Reader, w io.Writer) error { | ||
| d := logfmt.NewDecoder(r) | ||
| for d.ScanRecord() { // get record (line) | ||
| // Scan fields and get color | ||
| var fields [][2][]byte | ||
| var lineColor *color.Color | ||
| for d.ScanKeyval() { | ||
| fields = append(fields, [2][]byte{d.Key(), d.Value()}) | ||
| if string(d.Key()) == "level" { | ||
| lineColor = getLevelColor(string(d.Value())) | ||
| } | ||
| } | ||
|
|
||
| // Re-encode with color. We do not use logfmt Encoder because it does not support color | ||
| for i, field := range fields { | ||
| key := string(field[0]) | ||
| value := string(field[1]) | ||
|
|
||
| // Quote if needed | ||
| if strings.IndexFunc(value, needsQuotedValueRune) != -1 { | ||
| value = fmt.Sprintf("%q", value) | ||
| } | ||
|
|
||
| // Add color | ||
| if lineColor != nil { // handle case where no color (log level) was found | ||
| if key == "level" { | ||
| colorCopy := *lineColor | ||
| value = colorCopy.Add(color.Bold).Sprintf("%s", value) | ||
| } | ||
| key = lineColor.Sprintf("%s", field[0]) | ||
| } | ||
| if i != 0 { | ||
| fmt.Fprint(w, " ") | ||
| } | ||
| fmt.Fprintf(w, "%s=%s", key, value) | ||
| } | ||
| fmt.Fprintln(w) | ||
| } | ||
| if err := d.Err(); err != nil { | ||
| return fmt.Errorf("error processing logs: %v", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| type nopCloser struct { | ||
| io.Writer | ||
| } | ||
|
|
||
| func (nopCloser) Close() error { return nil } | ||
|
|
||
| // Print logfmt-formatted logs to stdout with color based on log level | ||
| // if color.NoColor is set, logs will be directly piped to stdout without processing | ||
| // Returns the writer to write logs to, and a waitgroup to wait for processing to finish | ||
| // Writer must be closed once all logs have been written | ||
| // Note: this function is a wrapper around processAndPrintLogs to avoid always creating a goroutine | ||
| func PrintLogsWithColor() (io.WriteCloser, *sync.WaitGroup) { | ||
| // If NoColor, do not parse logs and directly fall back to stdout | ||
| var wg sync.WaitGroup | ||
| if color.NoColor { | ||
| return nopCloser{os.Stdout}, &wg | ||
| } else { | ||
| // Else, create a goroutine to process logs. | ||
| wg.Add(1) | ||
| pr, pw := io.Pipe() | ||
|
|
||
| // Create coroutine to process logs | ||
| go func(pr *io.PipeReader) { | ||
| defer wg.Done() | ||
| err := processAndPrintLogs(pr, os.Stdout) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "error processing logs: %v\n", err) | ||
| } | ||
| }(pr) | ||
| return pw, &wg | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| Copyright the Velero contributors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
| package output | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| var LOG_LINE = "level=info msg=\"This is a test log\" key1=value1 key2=\"value with spaces\"" | ||
| var LOG_LINE_2 = "level=INVALID msg=\"This is a second test log\" key1=value1 key2=\"value 2 with spaces\"" | ||
| var LOG_LINE_3 = "level=error msg=\"This is a thirdtest log\" key1=value1 key2=\"value 3 with spaces\"" | ||
|
|
||
| // Note that all comparisons in this file work because color.NoColor is set to false by default, and thus no colors are added, even | ||
| // through the color adding code is run. | ||
|
|
||
| func TestColoredLogHasLog(t *testing.T) { | ||
| inputBuf := &bytes.Buffer{} | ||
| inputBuf.WriteString(LOG_LINE) | ||
|
|
||
| outputBuf := &bytes.Buffer{} | ||
| err := processAndPrintLogs(inputBuf, outputBuf) | ||
| if err != nil { | ||
| t.Fatalf("processAndPrintLogs returned error: %v", err) | ||
| } | ||
|
|
||
| assert.Contains(t, outputBuf.String(), "This is a test log") | ||
| } | ||
|
|
||
| // Test log line is unchanged since log is decomposed and re-composed | ||
| func TestColoredLogIsSameAsUncoloredLog(t *testing.T) { | ||
| inputBuf := &bytes.Buffer{} | ||
| inputBuf.WriteString(LOG_LINE) | ||
|
|
||
| outputBuf := &bytes.Buffer{} | ||
| err := processAndPrintLogs(inputBuf, outputBuf) | ||
| if err != nil { | ||
| t.Fatalf("processAndPrintLogs returned error: %v", err) | ||
| } | ||
|
|
||
| assert.Equal(t, LOG_LINE+"\n", outputBuf.String()) | ||
| } | ||
|
|
||
| // Test all log lines are sent correctly (and unchanged) | ||
| func TestMultipleColoredLogs(t *testing.T) { | ||
| inputBuf := &bytes.Buffer{} | ||
| inputBuf.WriteString(LOG_LINE) | ||
| inputBuf.WriteString("\n") | ||
| inputBuf.WriteString(LOG_LINE_2) | ||
| inputBuf.WriteString("\n") | ||
| inputBuf.WriteString(LOG_LINE_3) | ||
|
|
||
| outputBuf := &bytes.Buffer{} | ||
| err := processAndPrintLogs(inputBuf, outputBuf) | ||
| if err != nil { | ||
| t.Fatalf("processAndPrintLogs returned error: %v", err) | ||
| } | ||
|
|
||
| assert.Equal(t, fmt.Sprintf("%v\n%v\n%v\n", LOG_LINE, LOG_LINE_2, LOG_LINE_3), outputBuf.String()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.