Skip to content
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

fix(logs) logs should not color words with the ERR substring #1350

Merged
merged 6 commits into from
Sep 2, 2020
Merged
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
17 changes: 14 additions & 3 deletions internal/pkg/aws/cloudwatchlogs/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ package cloudwatchlogs
import (
"encoding/json"
"fmt"
"strings"
"regexp"

"github.com/aws/copilot-cli/internal/pkg/term/color"
c "github.com/fatih/color"
)

const (
Expand All @@ -36,10 +37,10 @@ func (l *Event) JSONString() (string, error) {
// HumanString returns the stringified LogEvent struct with human readable format.
func (l *Event) HumanString() string {
for _, code := range fatalCodes {
l.Message = strings.ReplaceAll(l.Message, code, color.Red.Sprint(code))
l.Message = colorCodeMessage(l.Message, code, color.Red)
}
for _, code := range warningCodes {
l.Message = strings.ReplaceAll(l.Message, code, color.Yellow.Sprint(code))
l.Message = colorCodeMessage(l.Message, code, color.Yellow)
}
return fmt.Sprintf("%s %s\n", color.Grey.Sprint(l.shortLogStreamName()), l.Message)
}
Expand All @@ -50,3 +51,13 @@ func (l *Event) shortLogStreamName() string {
}
return l.LogStreamName[0:shortLogStreamNameLength]
}

// colorCodeMessage returns the given message with color applied to every occurence of code
func colorCodeMessage(message string, code string, colorToApply *c.Color) string {
if c.NoColor {
return message
}
pattern := fmt.Sprintf("\\b%s\\b", code)
re := regexp.MustCompile(pattern)
return re.ReplaceAllString(message, colorToApply.Sprint(code))
}
61 changes: 61 additions & 0 deletions internal/pkg/aws/cloudwatchlogs/event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package cloudwatchlogs

import (
"fmt"
"testing"

"github.com/aws/copilot-cli/internal/pkg/term/color"
c "github.com/fatih/color"
"github.com/stretchr/testify/require"
)

func TestColorCodeMessage(t *testing.T) {
color.DisableColorBasedOnEnvVar()
testCases := map[string]struct {
givenMessage string
givenCode string
givenColor *c.Color
wantMessage string
}{
"should not apply color to a fatal code if it exists in a message as a substring": {
givenMessage: "e2e environment variables have been OVERRIDEN",
givenCode: "ERR",
givenColor: color.Red,
wantMessage: "e2e environment variables have been OVERRIDEN",
},
"should apply color to a fatal code if exists in a message": {
givenMessage: "An Error has occured",
givenCode: "Error",
givenColor: color.Red,
wantMessage: fmt.Sprintf("An %s has occured", color.Red.Sprint("Error")),
},
"should not apply color to a warning code if exists in a message as a substring": {
givenMessage: "Forewarning",
givenCode: "warning",
givenColor: color.Yellow,
wantMessage: "Forewarning",
},
"should apply color to a warning code if exists in a message": {
givenMessage: "Warning something has happened",
givenCode: "Warning",
givenColor: color.Yellow,
wantMessage: fmt.Sprintf("%s something has happened", color.Yellow.Sprint("Warning")),
},
"should apply color to a fatal code if code is next to special character": {
givenMessage: "error: something happened",
givenCode: "error",
givenColor: color.Red,
wantMessage: fmt.Sprintf("%s: something happened", color.Red.Sprint("error")),
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
color.DisableColorBasedOnEnvVar()
got := colorCodeMessage(tc.givenMessage, tc.givenCode, tc.givenColor)
require.Equal(t, tc.wantMessage, got)
})
}
}