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"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"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")))},
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 {
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"
)
var logCore *zapcore.Core
var (
logCore *zapcore.Core
loggerton *zap.SugaredLogger
)
func coreSingleton() *zapcore.Core {
if logCore == nil {
// level handling
highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
type logLevel int
const (
Development logLevel = iota
Info
Warn
Production
)
func singleton(level logLevel) *zap.SugaredLogger {
if loggerton != nil {
return loggerton
}
// 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
}
})
lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl < zapcore.ErrorLevel
})
// level-based output
consoleDebugging := zapcore.Lock(os.Stdout)
consoleErrors := zapcore.Lock(os.Stderr)
// encoder type
out := zapcore.Lock(os.Stderr)
consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
// combine into a logger core
core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, consoleErrors, highPriority),
zapcore.NewCore(consoleEncoder, consoleDebugging, lowPriority),
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)
}
return logCore
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
@ -40,16 +80,15 @@ const ctxKey loggingKey = "corsoLogger"
// Seed embeds a logger into the context for later retrieval.
func Seed(ctx context.Context) (context.Context, *zap.SugaredLogger) {
l := zap.New(*coreSingleton())
s := l.Sugar()
return context.WithValue(ctx, ctxKey, s), s
l := singleton(0)
return context.WithValue(ctx, ctxKey, l), l
}
// Ctx retrieves the logger embedded in the context.
func Ctx(ctx context.Context) *zap.SugaredLogger {
l := ctx.Value(ctxKey)
if l == nil {
return zap.New(*coreSingleton()).Sugar()
return singleton(0)
}
return l.(*zap.SugaredLogger)
}