Return error when log-format is invalid
parent
e647995a38
commit
ec252d06c9
|
@ -14,6 +14,8 @@
|
|||
|
||||
package logutil
|
||||
|
||||
import "fmt"
|
||||
|
||||
const (
|
||||
JsonLogFormat = "json"
|
||||
ConsoleLogFormat = "console"
|
||||
|
@ -21,11 +23,14 @@ const (
|
|||
|
||||
var DefaultLogFormat = JsonLogFormat
|
||||
|
||||
// ConvertToZapFormat converts log level string to zapcore.Level.
|
||||
func ConvertToZapFormat(format string) string {
|
||||
if format == ConsoleLogFormat {
|
||||
return format
|
||||
// ConvertToZapFormat converts and validated log format string.
|
||||
func ConvertToZapFormat(format string) (string, error) {
|
||||
switch format {
|
||||
case ConsoleLogFormat:
|
||||
return ConsoleLogFormat, nil
|
||||
case JsonLogFormat:
|
||||
return DefaultLogFormat, nil
|
||||
default:
|
||||
return "", fmt.Errorf("unknown log format: %s, supported values json, console", format)
|
||||
}
|
||||
|
||||
return DefaultLogFormat
|
||||
}
|
||||
|
|
|
@ -14,22 +14,31 @@
|
|||
|
||||
package logutil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLogFormat(t *testing.T) {
|
||||
tests := []struct {
|
||||
given string
|
||||
want string
|
||||
given string
|
||||
want string
|
||||
errExpected bool
|
||||
}{
|
||||
{"json", JsonLogFormat},
|
||||
{"console", ConsoleLogFormat},
|
||||
{"", JsonLogFormat},
|
||||
{"json", JsonLogFormat, false},
|
||||
{"console", ConsoleLogFormat, false},
|
||||
{"", "", true},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
got := ConvertToZapFormat(tt.given)
|
||||
got, err := ConvertToZapFormat(tt.given)
|
||||
if got != tt.want {
|
||||
t.Errorf("#%d: ConvertToZapFormat failure: want=%v, got=%v", i, tt.want, got)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
if !tt.errExpected {
|
||||
t.Errorf("#%d: ConvertToZapFormat unexpected error: %v", i, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ var DefaultZapLoggerConfig = zap.Config{
|
|||
Thereafter: 100,
|
||||
},
|
||||
|
||||
Encoding: ConvertToZapFormat(DefaultLogFormat),
|
||||
Encoding: DefaultLogFormat,
|
||||
|
||||
// copied from "zap.NewProductionEncoderConfig" with some updates
|
||||
EncoderConfig: zapcore.EncoderConfig{
|
||||
|
|
|
@ -106,7 +106,11 @@ func (cfg *Config) setupLogging() error {
|
|||
copied.ErrorOutputPaths = errOutputPaths
|
||||
copied = logutil.MergeOutputPaths(copied)
|
||||
copied.Level = zap.NewAtomicLevelAt(logutil.ConvertToZapLevel(cfg.LogLevel))
|
||||
copied.Encoding = logutil.ConvertToZapFormat(cfg.LogFormat)
|
||||
encoding, err := logutil.ConvertToZapFormat(cfg.LogFormat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
copied.Encoding = encoding
|
||||
if cfg.ZapLoggerBuilder == nil {
|
||||
lg, err := copied.Build()
|
||||
if err != nil {
|
||||
|
@ -132,7 +136,12 @@ func (cfg *Config) setupLogging() error {
|
|||
lvl := zap.NewAtomicLevelAt(logutil.ConvertToZapLevel(cfg.LogLevel))
|
||||
|
||||
var encoder zapcore.Encoder
|
||||
if logutil.ConvertToZapFormat(cfg.LogFormat) == logutil.ConsoleLogFormat {
|
||||
encoding, err := logutil.ConvertToZapFormat(cfg.LogFormat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if encoding == logutil.ConsoleLogFormat {
|
||||
encoder = zapcore.NewConsoleEncoder(logutil.DefaultZapLoggerConfig.EncoderConfig)
|
||||
} else {
|
||||
encoder = zapcore.NewJSONEncoder(logutil.DefaultZapLoggerConfig.EncoderConfig)
|
||||
|
|
Loading…
Reference in New Issue