Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions adapters/svc1zap/svc1zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

type svc1zapCore struct {
log svc1log.Logger
mutator func(entry zapcore.Entry) (zapcore.Entry, bool)
originFromCallLine bool
newParamFunc func(key string, value interface{}) svc1log.Param
}
Expand Down Expand Up @@ -64,6 +65,12 @@ func WithNewParamFunc(newParam func(key string, value interface{}) svc1log.Param
return func(core *svc1zapCore) { core.newParamFunc = newParam }
}

// WithEntryMutatorFunc provides a function for modifying or skipping entries dynamically.
// If mutator is set, ok must return true for the message to be logged.
func WithEntryMutatorFunc(mutator func(entry zapcore.Entry) (out zapcore.Entry, ok bool)) Option {
return func(core *svc1zapCore) { core.mutator = mutator }
}

func (c svc1zapCore) Enabled(level zapcore.Level) bool {
if checker, ok := c.log.(wlog.LevelChecker); ok {
switch level {
Expand All @@ -85,6 +92,13 @@ func (c svc1zapCore) With(fields []zapcore.Field) zapcore.Core {
}

func (c svc1zapCore) Check(entry zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if c.mutator != nil {
var ok bool
entry, ok = c.mutator(entry)
if !ok {
return ce
}
}
return ce.AddCore(entry, c)
}

Expand Down
34 changes: 34 additions & 0 deletions adapters/svc1zap/svc1zap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

// Use zap as logger implementation
_ "github.com/palantir/witchcraft-go-logging/wlog-zap"
Expand Down Expand Up @@ -106,6 +107,39 @@ func TestSvc1ZapWrapper(t *testing.T) {
logr4.Debug("logr 4")
assert.Empty(t, buf.String())
})

t.Run("logger with mutator", func(t *testing.T) {
buf := new(bytes.Buffer)
logger := svc1log.New(buf, wlog.DebugLevel)
logr5 := New(logger, WithEntryMutatorFunc(func(entry zapcore.Entry) (out zapcore.Entry, ok bool) {
if entry.Message == "skip me" {
return entry, false
}
if entry.Message == "drop to debug" {
entry.Level = zapcore.DebugLevel
}
return entry, true
}))
logr5.Info("logr 5")
logr5.Warn("skip me")
logr5.Warn("drop to debug")

lines := bytes.Split(bytes.TrimSpace(buf.Bytes()), []byte("\n"))
if assert.Len(t, lines, 2) {
assertLogLine(t, lines[0], objmatcher.MapMatcher{
"type": objmatcher.NewEqualsMatcher(svc1log.TypeValue),
"time": objmatcher.NewRegExpMatcher(".+"),
"level": objmatcher.NewEqualsMatcher("INFO"),
"message": objmatcher.NewEqualsMatcher("logr 5"),
})
assertLogLine(t, lines[1], objmatcher.MapMatcher{
"type": objmatcher.NewEqualsMatcher(svc1log.TypeValue),
"time": objmatcher.NewRegExpMatcher(".+"),
"level": objmatcher.NewEqualsMatcher("DEBUG"),
"message": objmatcher.NewEqualsMatcher("drop to debug"),
})
}
})
}

func assertLogLine(t *testing.T, logLine []byte, matcher objmatcher.MapMatcher) {
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-199.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: 'svc1zap: Add WithEntryMutatorFunc for dynamically modifying or skipping entries'
links:
- https://github.com/palantir/witchcraft-go-logging/pull/199