-
Notifications
You must be signed in to change notification settings - Fork 12
feature: Add adapters/svc1zap for code that accepts *zap.Logger #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| // Copyright (c) 2022 Palantir Technologies. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package svc1zap | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/palantir/witchcraft-go-logging/internal/gopath" | ||
| "github.com/palantir/witchcraft-go-logging/wlog" | ||
| "github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log" | ||
| "go.uber.org/zap" | ||
| "go.uber.org/zap/zapcore" | ||
| ) | ||
|
|
||
| type svc1zapCore struct { | ||
| log svc1log.Logger | ||
| originFromCallLine bool | ||
| newParamFunc func(key string, value interface{}) svc1log.Param | ||
| } | ||
|
|
||
| // New returns a zap logger that delegates to the provided svc1log logger. | ||
| // Enabled/disabled level configuration on the zap logger is ignored in favor of the svc1log configuration. | ||
bmoylan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| func New(logger svc1log.Logger, opts ...Option) *zap.Logger { | ||
| core := NewCore(logger, opts...) | ||
| z := zap.New(core) | ||
| if core.(*svc1zapCore).originFromCallLine { | ||
| z.WithOptions(zap.AddCaller()) | ||
| } | ||
| return z | ||
| } | ||
|
|
||
| func NewCore(logger svc1log.Logger, opts ...Option) zapcore.Core { | ||
| core := &svc1zapCore{log: logger} | ||
| for _, opt := range opts { | ||
| opt(core) | ||
| } | ||
| return core | ||
| } | ||
|
|
||
| type Option func(*svc1zapCore) | ||
|
|
||
| // WithOriginFromZapCaller enables zap.AddCaller() and uses the caller file and line to construct the origin value. | ||
| // Similar to svc1log.OriginFromCallLine(). | ||
| func WithOriginFromZapCaller() Option { | ||
| return func(core *svc1zapCore) { core.originFromCallLine = true } | ||
| } | ||
|
|
||
| // WithNewParamFunc provides a function for constructing svc1log.Param values from zap fields. | ||
| // Use this option to control parameter safety. By default, all fields are converted to unsafe params. | ||
| // If newParam returns nil, the field is skipped. | ||
| func WithNewParamFunc(newParam func(key string, value interface{}) svc1log.Param) Option { | ||
| return func(core *svc1zapCore) { core.newParamFunc = newParam } | ||
| } | ||
|
|
||
| func (c svc1zapCore) Enabled(level zapcore.Level) bool { | ||
| if checker, ok := c.log.(wlog.LevelChecker); ok { | ||
| switch level { | ||
| case zapcore.DebugLevel: | ||
| return checker.Enabled(wlog.DebugLevel) | ||
| case zapcore.InfoLevel: | ||
| return checker.Enabled(wlog.InfoLevel) | ||
| case zapcore.WarnLevel: | ||
| return checker.Enabled(wlog.WarnLevel) | ||
| default: | ||
| return checker.Enabled(wlog.ErrorLevel) | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func (c svc1zapCore) With(fields []zapcore.Field) zapcore.Core { | ||
| return svc1zapCore{log: svc1log.WithParams(c.log, c.fieldsToWlogParams(fields)...)} | ||
| } | ||
|
|
||
| func (c svc1zapCore) Check(entry zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry { | ||
| return ce.AddCore(entry, c) | ||
| } | ||
|
|
||
| func (c svc1zapCore) Write(entry zapcore.Entry, fields []zapcore.Field) error { | ||
| message := formatMessage(entry) | ||
| params := c.fieldsToWlogParams(fields) | ||
| if c.originFromCallLine && entry.Caller.Defined { | ||
| params = append(params, svc1log.Origin(gopath.TrimPrefix(entry.Caller.FullPath()))) | ||
| } | ||
| switch entry.Level { | ||
| case zapcore.DebugLevel: | ||
| c.log.Debug(message, params...) | ||
| case zapcore.InfoLevel: | ||
| c.log.Info(message, params...) | ||
| case zapcore.WarnLevel: | ||
| c.log.Warn(message, params...) | ||
| default: | ||
| c.log.Error(message, params...) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (c svc1zapCore) Sync() error { return nil } | ||
|
|
||
| func (c svc1zapCore) fieldsToWlogParams(fields []zapcore.Field) []svc1log.Param { | ||
| var params []svc1log.Param | ||
| for key, value := range fieldsToMap(fields) { | ||
| if c.newParamFunc != nil { | ||
| if p := c.newParamFunc(key, value); p != nil { | ||
| params = append(params, p) | ||
| } | ||
| } else { | ||
| params = append(params, svc1log.UnsafeParam(key, value)) | ||
| } | ||
| } | ||
| return params | ||
| } | ||
|
|
||
| func formatMessage(entry zapcore.Entry) string { | ||
| if entry.LoggerName == "" { | ||
| return entry.Message | ||
| } | ||
| sb := strings.Builder{} | ||
| sb.Grow(len(entry.LoggerName) + 2 + len(entry.Message)) | ||
| sb.WriteString(entry.LoggerName) | ||
| sb.WriteString(": ") | ||
| sb.WriteString(entry.Message) | ||
| return sb.String() | ||
| } | ||
|
|
||
| func fieldsToMap(fields []zapcore.Field) map[string]interface{} { | ||
| params := zapcore.NewMapObjectEncoder() | ||
| for _, field := range fields { | ||
| if field.Key == "token" { | ||
| continue // who logs a token...? | ||
| } | ||
| field.AddTo(params) | ||
| } | ||
| return params.Fields | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // Copyright (c) 2022 Palantir Technologies. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package svc1zap | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/palantir/pkg/objmatcher" | ||
| "github.com/palantir/witchcraft-go-logging/wlog" | ||
| "github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log" | ||
| "github.com/stretchr/testify/assert" | ||
| "go.uber.org/zap" | ||
|
|
||
| // Use zap as logger implementation | ||
| _ "github.com/palantir/witchcraft-go-logging/wlog-zap" | ||
| ) | ||
|
|
||
| func TestSvc1LogrWrapper(t *testing.T) { | ||
bmoylan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| prefixParamFunc := func(key string, value interface{}) svc1log.Param { | ||
| if strings.HasPrefix(key, "safe") { | ||
| return svc1log.SafeParam(key, value) | ||
| } | ||
| if !strings.HasPrefix(key, "forbidden") { | ||
| return svc1log.UnsafeParam(key, value) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| t.Run("defaults to all unsafe params", func(t *testing.T) { | ||
| buf := new(bytes.Buffer) | ||
| logger := svc1log.New(buf, wlog.DebugLevel) | ||
| logr1 := New(logger) | ||
| logr1.Info("logr 1", zap.String("safeString", "string"), zap.String("forbiddenToken", "token"), zap.Int("unsafeInt", 42)) | ||
| assertLogLine(t, buf.Bytes(), objmatcher.MapMatcher{ | ||
| "level": objmatcher.NewEqualsMatcher("INFO"), | ||
| "time": objmatcher.NewRegExpMatcher(".+"), | ||
| "message": objmatcher.NewEqualsMatcher("logr 1"), | ||
| "type": objmatcher.NewEqualsMatcher(svc1log.TypeValue), | ||
| "unsafeParams": objmatcher.MapMatcher{ | ||
| "forbiddenToken": objmatcher.NewEqualsMatcher("token"), | ||
| "safeString": objmatcher.NewEqualsMatcher("string"), | ||
| "unsafeInt": objmatcher.NewEqualsMatcher(float64(42)), | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("caller origin and custom params", func(t *testing.T) { | ||
| buf := new(bytes.Buffer) | ||
| logger := svc1log.New(buf, wlog.DebugLevel) | ||
| logr2 := New(logger, WithOriginFromZapCaller(), WithNewParamFunc(prefixParamFunc)).WithOptions(zap.AddCaller()) | ||
| logr2.Info("logr 2", zap.String("safeString", "string"), zap.String("forbiddenToken", "token"), zap.Int("unsafeInt", 42)) | ||
| assertLogLine(t, buf.Bytes(), objmatcher.MapMatcher{ | ||
| "level": objmatcher.NewEqualsMatcher("INFO"), | ||
| "time": objmatcher.NewRegExpMatcher(".+"), | ||
| "message": objmatcher.NewEqualsMatcher("logr 2"), | ||
| "type": objmatcher.NewEqualsMatcher(svc1log.TypeValue), | ||
| "origin": objmatcher.NewRegExpMatcher("^github.com/palantir/witchcraft-go-logging/adapters/svc1zap/svc1zap_test.go:\\d+"), | ||
| "params": objmatcher.MapMatcher{ | ||
| "safeString": objmatcher.NewEqualsMatcher("string"), | ||
| }, | ||
| "unsafeParams": objmatcher.MapMatcher{ | ||
| "unsafeInt": objmatcher.NewEqualsMatcher(float64(42)), | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("logger with attached params", func(t *testing.T) { | ||
| buf := new(bytes.Buffer) | ||
| logger := svc1log.New(buf, wlog.DebugLevel) | ||
| logr3 := New(logger).Named("logr3").With(zap.String("name", "logr3")) | ||
| logr3.Error("logr 3", zap.String("safeString", "string"), zap.String("forbiddenToken", "token"), zap.Int("unsafeInt", 42)) | ||
| assertLogLine(t, buf.Bytes(), objmatcher.MapMatcher{ | ||
| "level": objmatcher.NewEqualsMatcher("ERROR"), | ||
| "time": objmatcher.NewRegExpMatcher(".+"), | ||
| "message": objmatcher.NewEqualsMatcher("logr3: logr 3"), | ||
| "type": objmatcher.NewEqualsMatcher(svc1log.TypeValue), | ||
| "unsafeParams": objmatcher.MapMatcher{ | ||
| "forbiddenToken": objmatcher.NewEqualsMatcher("token"), | ||
| "name": objmatcher.NewEqualsMatcher("logr3"), | ||
| "safeString": objmatcher.NewEqualsMatcher("string"), | ||
| "unsafeInt": objmatcher.NewEqualsMatcher(float64(42)), | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("logger with disabled level", func(t *testing.T) { | ||
| buf := new(bytes.Buffer) | ||
| logger := svc1log.New(buf, wlog.InfoLevel) | ||
| logr4 := New(logger) | ||
| logr4.Debug("logr 4") | ||
| assert.Empty(t, buf.String()) | ||
| }) | ||
| } | ||
|
|
||
| func assertLogLine(t *testing.T, logLine []byte, matcher objmatcher.MapMatcher) { | ||
| logEntry := map[string]interface{}{} | ||
| err := json.Unmarshal(logLine, &logEntry) | ||
| assert.NoError(t, err) | ||
| assert.NoError(t, matcher.Matches(logEntry)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| type: feature | ||
| feature: | ||
| description: Add `adapters/svc1zap` for code that accepts `*zap.Logger` | ||
| links: | ||
| - https://github.com/palantir/witchcraft-go-logging/pull/198 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.