mirror of
https://github.com/gomods/athens
synced 2026-02-03 11:00:32 +00:00
Add log formatting settings (#1926)
Adds a log format setting as ATHENS_LOG_FORMAT that can be either plain or JSON when CloudRuntime is none (the default). Does not break or change any existing behavior.
This commit is contained in:
@@ -10,10 +10,10 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gomods/athens/pkg/download/mode"
|
||||
"github.com/gomods/athens/pkg/errors"
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
"gopkg.in/go-playground/validator.v9"
|
||||
)
|
||||
|
||||
const defaultConfigFile = "athens.toml"
|
||||
@@ -28,7 +28,8 @@ type Config struct {
|
||||
GoGetDir string `envconfig:"ATHENS_GOGET_DIR"`
|
||||
ProtocolWorkers int `validate:"required" envconfig:"ATHENS_PROTOCOL_WORKERS"`
|
||||
LogLevel string `validate:"required" envconfig:"ATHENS_LOG_LEVEL"`
|
||||
CloudRuntime string `validate:"required" envconfig:"ATHENS_CLOUD_RUNTIME"`
|
||||
LogFormat string `validate:"oneof='' 'json' 'plain'" envconfig:"ATHENS_LOG_FORMAT"`
|
||||
CloudRuntime string `validate:"required_without=LogFormat" envconfig:"ATHENS_CLOUD_RUNTIME"`
|
||||
EnablePprof bool `envconfig:"ATHENS_ENABLE_PPROF"`
|
||||
PprofPort string `envconfig:"ATHENS_PPROF_PORT"`
|
||||
FilterFile string `envconfig:"ATHENS_FILTER_FILE"`
|
||||
@@ -150,6 +151,7 @@ func defaultConfig() *Config {
|
||||
GoGetWorkers: 10,
|
||||
ProtocolWorkers: 30,
|
||||
LogLevel: "debug",
|
||||
LogFormat: "json",
|
||||
CloudRuntime: "none",
|
||||
EnablePprof: false,
|
||||
PprofPort: ":3001",
|
||||
|
||||
@@ -258,6 +258,7 @@ func TestParseExampleConfig(t *testing.T) {
|
||||
expConf := &Config{
|
||||
GoEnv: "development",
|
||||
LogLevel: "debug",
|
||||
LogFormat: "json",
|
||||
GoBinary: "go",
|
||||
GoGetWorkers: 10,
|
||||
ProtocolWorkers: 30,
|
||||
|
||||
+5
-1
@@ -64,6 +64,10 @@ func sortFields(data logrus.Fields) []string {
|
||||
return keys
|
||||
}
|
||||
|
||||
func getDefaultFormatter() logrus.Formatter {
|
||||
func parseFormat(format string) logrus.Formatter {
|
||||
if format == "plain" {
|
||||
return &logrus.TextFormatter{}
|
||||
}
|
||||
|
||||
return &logrus.JSONFormatter{}
|
||||
}
|
||||
|
||||
+2
-2
@@ -14,7 +14,7 @@ type Logger struct {
|
||||
// environment and the cloud platform it is
|
||||
// running on. TODO: take cloud arg and env
|
||||
// to construct the correct JSON formatter.
|
||||
func New(cloudProvider string, level logrus.Level) *Logger {
|
||||
func New(cloudProvider string, level logrus.Level, format string) *Logger {
|
||||
l := logrus.New()
|
||||
switch cloudProvider {
|
||||
case "GCP":
|
||||
@@ -22,7 +22,7 @@ func New(cloudProvider string, level logrus.Level) *Logger {
|
||||
case "none":
|
||||
l.Formatter = getDevFormatter()
|
||||
default:
|
||||
l.Formatter = getDefaultFormatter()
|
||||
l.Formatter = parseFormat(format)
|
||||
}
|
||||
l.Level = level
|
||||
return &Logger{Logger: l}
|
||||
|
||||
+6
-5
@@ -14,6 +14,7 @@ import (
|
||||
type input struct {
|
||||
name string
|
||||
cloudProvider string
|
||||
format string
|
||||
level logrus.Level
|
||||
fields logrus.Fields
|
||||
logFunc func(e Entry) time.Time
|
||||
@@ -82,10 +83,10 @@ var testCases = []input{
|
||||
output: `{"message":"warn message","severity":"warning","timestamp":"%v"}` + "\n",
|
||||
},
|
||||
{
|
||||
name: "default",
|
||||
cloudProvider: "default",
|
||||
level: logrus.DebugLevel,
|
||||
fields: logrus.Fields{"xyz": "abc", "abc": "xyz"},
|
||||
name: "default json",
|
||||
format: "json",
|
||||
level: logrus.DebugLevel,
|
||||
fields: logrus.Fields{"xyz": "abc", "abc": "xyz"},
|
||||
logFunc: func(e Entry) time.Time {
|
||||
t := time.Now()
|
||||
e.Warnf("warn message")
|
||||
@@ -98,7 +99,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, "")
|
||||
var buf bytes.Buffer
|
||||
lggr.Out = &buf
|
||||
e := lggr.WithFields(tc.fields)
|
||||
|
||||
@@ -24,7 +24,7 @@ func TestLogContext(t *testing.T) {
|
||||
r.HandleFunc("/test", h)
|
||||
|
||||
var buf bytes.Buffer
|
||||
lggr := log.New("", logrus.DebugLevel)
|
||||
lggr := log.New("", logrus.DebugLevel, "")
|
||||
lggr.Formatter = &logrus.JSONFormatter{DisableTimestamp: true}
|
||||
lggr.Out = &buf
|
||||
|
||||
|
||||
Reference in New Issue
Block a user