Skip to content

Commit

Permalink
Rework logging defaults (#1927)
Browse files Browse the repository at this point in the history
Changes default logger back to plain.
  • Loading branch information
matt0x6F committed Mar 28, 2024
1 parent b72f01b commit b9c8fb7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion config.dev.toml
Expand Up @@ -81,7 +81,7 @@ LogLevel = "debug"
# LogFormat determines the format that logs are output in. Defaults to json.
# It is only used when CloudRuntime is set to none. Values can be "plain" or "json".
# Env override: ATHENS_LOG_FORMAT
LogFormat = "json"
LogFormat = "plain"

# CloudRuntime is the Cloud Provider on which the Proxy/Registry is running.
# Currently available options are "GCP", or "none". Defaults to "none"
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Expand Up @@ -151,7 +151,7 @@ func defaultConfig() *Config {
GoGetWorkers: 10,
ProtocolWorkers: 30,
LogLevel: "debug",
LogFormat: "json",
LogFormat: "plain",
CloudRuntime: "none",
EnablePprof: false,
PprofPort: ":3001",
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config_test.go
Expand Up @@ -258,7 +258,7 @@ func TestParseExampleConfig(t *testing.T) {
expConf := &Config{
GoEnv: "development",
LogLevel: "debug",
LogFormat: "json",
LogFormat: "plain",
GoBinary: "go",
GoGetWorkers: 10,
ProtocolWorkers: 30,
Expand Down
6 changes: 3 additions & 3 deletions pkg/log/format.go
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
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
42 changes: 36 additions & 6 deletions pkg/log/log_test.go
Expand Up @@ -83,10 +83,36 @@ 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",
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 json",
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 +125,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" || (tc.format == "" && (tc.cloudProvider == "none" || tc.cloudProvider == "")) {
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 b9c8fb7

Please sign in to comment.