fix up logger specs (#489)

This commit is contained in:
Keepers 2022-08-04 17:28:26 -06:00 committed by GitHub
parent 2d6c765ee8
commit 73942fe952
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 32 deletions

View File

@ -6,6 +6,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
) )
@ -43,12 +44,6 @@ func (suite *GCStatusTestSuite) TestCreateStatus() {
params: statusParams{Restore, 12, 9, 8, WrapAndAppend("tres", errors.New("three"), WrapAndAppend("arc376", errors.New("one"), errors.New("two")))}, params: statusParams{Restore, 12, 9, 8, WrapAndAppend("tres", errors.New("three"), WrapAndAppend("arc376", errors.New("one"), errors.New("two")))},
expect: assert.True, expect: assert.True,
}, },
{
name: "Invalid status",
// todo: expect panic once logger.DPanicw identifies dev mode.
params: statusParams{Backup, 9, 3, 13, errors.New("invalidcl")},
expect: assert.True,
},
} }
for _, test := range table { for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) { suite.T().Run(test.name, func(t *testing.T) {
@ -64,3 +59,18 @@ func (suite *GCStatusTestSuite) TestCreateStatus() {
} }
} }
func (suite *GCStatusTestSuite) TestCreateStatus_InvalidStatus() {
t := suite.T()
params := statusParams{Backup, 9, 3, 13, errors.New("invalidcl")}
require.Panics(t, func() {
CreateStatus(
context.Background(),
params.operationType,
params.objects,
params.success,
params.folders,
params.err,
)
})
}

View File

@ -8,30 +8,70 @@ import (
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
) )
var logCore *zapcore.Core var (
logCore *zapcore.Core
loggerton *zap.SugaredLogger
)
func coreSingleton() *zapcore.Core { type logLevel int
if logCore == nil {
// level handling const (
highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { Development logLevel = iota
return lvl >= zapcore.ErrorLevel Info
}) Warn
lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { Production
return lvl < zapcore.ErrorLevel )
})
// level-based output func singleton(level logLevel) *zap.SugaredLogger {
consoleDebugging := zapcore.Lock(os.Stdout) if loggerton != nil {
consoleErrors := zapcore.Lock(os.Stderr) return loggerton
// encoder type
consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
// combine into a logger core
core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, consoleErrors, highPriority),
zapcore.NewCore(consoleEncoder, consoleDebugging, lowPriority),
)
logCore = &core
} }
return logCore
// set up a logger core to use as a fallback
levelFilter := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
switch level {
case Info:
return lvl >= zapcore.InfoLevel
case Warn:
return lvl >= zapcore.WarnLevel
case Production:
return lvl >= zapcore.ErrorLevel
default:
return true
}
})
out := zapcore.Lock(os.Stderr)
consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, out, levelFilter),
)
logCore = &core
// then try to set up a logger directly
var (
lgr *zap.Logger
err error
)
if level != Production {
cfg := zap.NewDevelopmentConfig()
switch level {
case Info:
cfg.Level = zap.NewAtomicLevelAt(zapcore.InfoLevel)
case Warn:
cfg.Level = zap.NewAtomicLevelAt(zapcore.WarnLevel)
}
lgr, err = cfg.Build()
} else {
lgr, err = zap.NewProduction()
}
// fall back to the core config if the default creation fails
if err != nil {
lgr = zap.New(*logCore)
}
loggerton = lgr.Sugar()
return loggerton
} }
type loggingKey string type loggingKey string
@ -40,16 +80,15 @@ const ctxKey loggingKey = "corsoLogger"
// Seed embeds a logger into the context for later retrieval. // Seed embeds a logger into the context for later retrieval.
func Seed(ctx context.Context) (context.Context, *zap.SugaredLogger) { func Seed(ctx context.Context) (context.Context, *zap.SugaredLogger) {
l := zap.New(*coreSingleton()) l := singleton(0)
s := l.Sugar() return context.WithValue(ctx, ctxKey, l), l
return context.WithValue(ctx, ctxKey, s), s
} }
// Ctx retrieves the logger embedded in the context. // Ctx retrieves the logger embedded in the context.
func Ctx(ctx context.Context) *zap.SugaredLogger { func Ctx(ctx context.Context) *zap.SugaredLogger {
l := ctx.Value(ctxKey) l := ctx.Value(ctxKey)
if l == nil { if l == nil {
return zap.New(*coreSingleton()).Sugar() return singleton(0)
} }
return l.(*zap.SugaredLogger) return l.(*zap.SugaredLogger)
} }