From 63170102d2bf733457e3e1525f195b292715d554 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Thu, 27 May 2021 23:18:39 +0000 Subject: [PATCH] feat: add 'testing' preset --- encoder.go | 4 ++-- zapconfig.go | 8 +++++++- zapconfig_test.go | 10 ++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/encoder.go b/encoder.go index 022615d..95a307c 100644 --- a/encoder.go +++ b/encoder.go @@ -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)) diff --git a/zapconfig.go b/zapconfig.go index 17b9807..9581957 100644 --- a/zapconfig.go +++ b/zapconfig.go @@ -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 { @@ -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 } diff --git a/zapconfig_test.go b/zapconfig_test.go index 968bbdc..92c4193 100644 --- a/zapconfig_test.go +++ b/zapconfig_test.go @@ -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) {