Skip to content

Commit

Permalink
feat: add 'testing' preset
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed May 27, 2021
1 parent 55b8dd5 commit 6317010
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"go.uber.org/zap/zapcore"
)

/*func stableWidthCapitalLevelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
func stableWidthCapitalLevelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(fmt.Sprintf("%-5s", l.CapitalString()))
}*/
}

func stableWidthNameEncoder(loggerName string, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(fmt.Sprintf("%-18s", loggerName))
Expand Down
8 changes: 7 additions & 1 deletion zapconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const (
)

// AvailablePresets is the list of preset supported by `SetPreset`.
var AvailablePresets = []string{"console", "json", "light-console", "light-json"}
var AvailablePresets = []string{"console", "json", "light-console", "light-json", "testing"}

// SetPreset configures various things based on just a keyword.
func (c *Configurator) SetPreset(name string) *Configurator {
Expand All @@ -80,6 +80,12 @@ func (c *Configurator) SetPreset(name string) *Configurator {
c.opts.EncoderConfig = zap.NewProductionEncoderConfig()
c.opts.Encoding = jsonEncoding
c.opts.EncoderConfig.TimeKey = ""
case "testing":
c.opts.EncoderConfig = zap.NewDevelopmentEncoderConfig()
c.opts.Encoding = consoleEncoding
c.opts.EncoderConfig.TimeKey = ""
c.opts.EncoderConfig.EncodeLevel = stableWidthCapitalLevelEncoder
c.opts.EncoderConfig.EncodeName = stableWidthNameEncoder
default:
c.configErr = multierr.Append(c.configErr, fmt.Errorf("unknown preset: %q", name)) // nolint:goerr113
}
Expand Down
10 changes: 10 additions & 0 deletions zapconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ func TestBuilder(t *testing.T) {
require.NotEqual(t, output[1], `{"level":"info","logger":"foobar","caller":"zapconfig/zapconfig_test.go:17","msg":"hello world!","baz":42}`)
},
},
{"set-preset-testing",
func(config *Configurator) {
config.SetPreset("testing")
},
func(t *testing.T, config *zap.Config, output []string, err error) {
require.NoError(t, err)
require.Equal(t, "DEBUG\tzapconfig/zapconfig_test.go:16\thello world!\t{\"foo\": \"bar\"}", output[0])
require.Equal(t, "INFO \tfoobar \tzapconfig/zapconfig_test.go:17\thello world!\t{\"baz\": 42}", output[1])
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit 6317010

Please sign in to comment.