mirror of
https://github.com/gomods/athens
synced 2026-02-03 09:50:31 +00:00
+1
-1
@@ -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"
|
||||
|
||||
@@ -151,7 +151,7 @@ func defaultConfig() *Config {
|
||||
GoGetWorkers: 10,
|
||||
ProtocolWorkers: 30,
|
||||
LogLevel: "debug",
|
||||
LogFormat: "json",
|
||||
LogFormat: "plain",
|
||||
CloudRuntime: "none",
|
||||
EnablePprof: false,
|
||||
PprofPort: ":3001",
|
||||
|
||||
@@ -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,
|
||||
|
||||
+3
-3
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
+36
-6
@@ -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")
|
||||
@@ -99,7 +125,7 @@ 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)
|
||||
@@ -107,7 +133,11 @@ func TestCloudLogger(t *testing.T) {
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user