Skip to content

Commit

Permalink
Rework logging defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
matt0x6F committed Mar 22, 2024
1 parent 08520bf commit d080542
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
6 changes: 3 additions & 3 deletions pkg/log/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ func sortFields(data logrus.Fields) []string {
}

func parseFormat(format string) logrus.Formatter {
if format == "plain" {
return &logrus.TextFormatter{}
if format == "json" {
return &logrus.JSONFormatter{}
}

return &logrus.JSONFormatter{}
return getDevFormatter()
}
2 changes: 0 additions & 2 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ func New(cloudProvider string, level logrus.Level, format string) *Logger {
switch cloudProvider {
case "GCP":
l.Formatter = getGCPFormatter()
case "none":
l.Formatter = getDevFormatter()
default:
l.Formatter = parseFormat(format)
}
Expand Down
30 changes: 24 additions & 6 deletions pkg/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,24 @@ var testCases = []input{
output: `{"message":"warn message","severity":"warning","timestamp":"%v"}` + "\n",
},
{
name: "default json",
format: "json",
level: logrus.DebugLevel,
fields: logrus.Fields{"xyz": "abc", "abc": "xyz"},
name: "default plain",
format: "plain",
cloudProvider: "none",
level: logrus.DebugLevel,
fields: logrus.Fields{"xyz": "abc", "abc": "xyz"},
logFunc: func(e Entry) time.Time {
t := time.Now()
e.Warnf("warn message")
return t
},
output: `WARNING[%v]: warn message` + "\t" + `abc=xyz xyz=abc` + " \n",
},
{
name: "default",
format: "json",
cloudProvider: "none",
level: logrus.DebugLevel,
fields: logrus.Fields{"xyz": "abc", "abc": "xyz"},
logFunc: func(e Entry) time.Time {
t := time.Now()
e.Warnf("warn message")
Expand All @@ -99,15 +113,19 @@ var testCases = []input{
func TestCloudLogger(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
lggr := New(tc.cloudProvider, tc.level, "")
lggr := New(tc.cloudProvider, tc.level, tc.format)
var buf bytes.Buffer
lggr.Out = &buf
e := lggr.WithFields(tc.fields)
entryTime := tc.logFunc(e)
out := buf.String()
expected := tc.output
if strings.Contains(expected, "%v") {
expected = fmt.Sprintf(expected, entryTime.Format(time.RFC3339))
if tc.format == "plain" {
expected = fmt.Sprintf(expected, entryTime.Format(time.Kitchen))
} else {
expected = fmt.Sprintf(expected, entryTime.Format(time.RFC3339))
}
}

require.Equal(t, expected, out, "expected the logged entry to match the testCase output")
Expand Down

0 comments on commit d080542

Please sign in to comment.