Skip to content

Commit

Permalink
feat: pass string version of state to logger
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Dec 24, 2024
1 parent 2402c0d commit 3a4e531
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 42 deletions.
5 changes: 4 additions & 1 deletion pkg/log/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ func (f *CustomFormatter) Format(entry *logrus.Entry) ([]byte, error) { //nolint
if _, ok := entry.Data["state"]; !ok {
return f.FallbackFormatter.Format(entry)
}
if _, ok := entry.Data["state_code"]; !ok {
return f.FallbackFormatter.Format(entry)
}
if _, ok := entry.Data["name"]; !ok {
return f.FallbackFormatter.Format(entry)
}

Check warning on line 46 in pkg/log/formatter.go

View check run for this annotation

Codecov / codecov/patch

pkg/log/formatter.go#L45-L46

Added lines #L45 - L46 were not covered by tests

owner := entry.Data["owner"].(string)
resourceName := entry.Data["name"].(string)
state := entry.Data["state"].(int)
state := entry.Data["state_code"].(int)

var sortedFields = make([]string, 0)
for k, v := range entry.Data {
Expand Down
88 changes: 51 additions & 37 deletions pkg/log/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

func TestCustomFormatter_Format(t *testing.T) {
logrus.StandardLogger().ReplaceHooks(make(logrus.LevelHooks))

cases := []struct {
name string
input *logrus.Entry
Expand Down Expand Up @@ -101,12 +103,13 @@ func TestCustomFormatter_Format(t *testing.T) {
input: &logrus.Entry{
Message: "would remove",
Data: logrus.Fields{
"type": "test",
"owner": "owner",
"name": "resource",
"state": 0,
"prop:one": "1",
"prop:two": "2",
"type": "test",
"owner": "owner",
"name": "resource",
"state": "new",
"state_code": 0,
"prop:one": "1",
"prop:two": "2",
},
},
want: []byte(fmt.Sprintf("%s - %s - %s - %s - %s\n",
Expand All @@ -124,7 +127,8 @@ func TestCustomFormatter_Format(t *testing.T) {
"type": "test",
"owner": "owner",
"name": "resource",
"state": 2,
"state": "hold",
"state_code": 2,
"prop:one": "1",
"prop:two": "2",
"prop:_tagPrefix": "tag",
Expand All @@ -142,12 +146,13 @@ func TestCustomFormatter_Format(t *testing.T) {
input: &logrus.Entry{
Message: "test message",
Data: logrus.Fields{
"type": "test",
"owner": "owner",
"name": "resource",
"state": 3,
"prop:one": "1",
"prop:two": "2",
"type": "test",
"owner": "owner",
"name": "resource",
"state": "pending",
"state_code": 3,
"prop:one": "1",
"prop:two": "2",
},
},
want: []byte(fmt.Sprintf("%s - %s - %s - %s - %s\n",
Expand Down Expand Up @@ -188,44 +193,52 @@ func TestCustomFormatter_FormatReasons(t *testing.T) {
}

cases := []struct {
name string
state int
color color.Color
name string
state string
stateCode int
color color.Color
}{
{
name: "reason-success",
state: 0,
color: ReasonSuccess,
name: "reason-success",
state: "new",
stateCode: 0,
color: ReasonSuccess,
},
{
name: "reason-hold",
state: 2,
color: ReasonHold,
name: "reason-hold",
state: "hold",
stateCode: 2,
color: ReasonHold,
},
{
name: "reason-remove-triggered",
state: 3,
color: ReasonRemoveTriggered,
name: "reason-remove-triggered",
state: "pending",
stateCode: 3,
color: ReasonRemoveTriggered,
},
{
name: "reason-wait-dependency",
state: 4,
color: ReasonWaitDependency,
name: "reason-wait-dependency",
state: "pending-dependency",
stateCode: 4,
color: ReasonWaitDependency,
},
{
name: "reason-wait-pending",
state: 5,
color: ReasonWaitPending,
name: "reason-wait-pending",
state: "waiting",
stateCode: 5,
color: ReasonWaitPending,
},
{
name: "reason-error",
state: 6,
color: ReasonError,
name: "reason-error",
state: "failed",
stateCode: 6,
color: ReasonError,
},
{
name: "reason-skip",
state: 7,
color: ReasonSkip,
name: "reason-skip",
state: "filtered",
stateCode: 7,
color: ReasonSkip,
},
}

Expand All @@ -242,6 +255,7 @@ func TestCustomFormatter_FormatReasons(t *testing.T) {

newTestEntry := testEntry
newTestEntry.Data["state"] = tc.state
newTestEntry.Data["state_code"] = tc.stateCode

got, err := cf.Format(newTestEntry)
assert.NoError(t, err)
Expand Down
7 changes: 4 additions & 3 deletions pkg/queue/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ func (i *Item) Print() {
}

itemLog := i.Logger.WithFields(logrus.Fields{
"owner": i.Owner,
"type": i.Type,
"state": int(i.State),
"owner": i.Owner,
"type": i.Type,
"state": i.State.String(),
"state_code": int(i.State),
})

rString, ok := i.Resource.(resource.LegacyStringer)
Expand Down
2 changes: 1 addition & 1 deletion pkg/queue/item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func Test_ItemLoggerCustom(t *testing.T) {
hookCalled = true
assert.Equal(t, "us-east-1", e.Data["owner"])
assert.Equal(t, "TestResource", e.Data["type"])
assert.Equal(t, 0, e.Data["state"])
assert.Equal(t, 0, e.Data["state_code"])
assert.Equal(t, "would remove", e.Message)
},
})
Expand Down

0 comments on commit 3a4e531

Please sign in to comment.