Skip to content
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

Improvement: Add gologr svclog wrapper #114

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ witchcraft-go-logging
=====================
[![](https://godoc.org/github.com/palantir/witchcraft-go-logging?status.svg)](http://godoc.org/github.com/palantir/witchcraft-go-logging)

`witchcraft-go-logging` is a Go implementation of the Witchcraft logging specification. It provides an API that can be
used for logging and some default implementations of the logging API using different existing popular Go logging
libraries. `witchcraft-go-logging` includes implementations that use [zap](https://github.com/uber-go/zap),
[zerolog](https://github.com/rs/zerolog) and [glog](https://github.com/golang/glog).
`witchcraft-go-logging` is a Go implementationof the Witchcraft logging specification. It provides an API that can be
used for logging and some default implementation and adapters

**Implementations** wrap an existing go logging library to implement the wlog interface. We currently provide
- [zap](https://github.com/uber-go/zap),
- [zerolog](https://github.com/rs/zerolog),
- [glog](https://github.com/golang/glog).

**Adapters** wrap our go logging implementation (svc1log, ev2log, req2log) to implement a go logging interface. We currently provide
- [go-logr](https://github.com/go-logr/logr) interface that uses svc1log inside.

Architecture
------------
Expand Down
101 changes: 101 additions & 0 deletions adapters/svc1logr/svc1logr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) 2021 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 svc1logr

import (
"fmt"
"path"

"github.com/go-logr/logr"
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log"
)

type svc1logr struct {
origin string
logger svc1log.Logger
level int
enabled bool
}

// New provides an implementation of [logr.Logger](https://pkg.go.dev/github.com/go-logr/logr#Logger) which delegates to
// a witchcraft svc1log logger. Use this package for software which expects a logr.Logger but where the calling code
// has access to an svc1log.Logger or wants the output to be Witchcraft-compatible. Info() is logged as INFO and
// Error() as ERROR. All values are converted to safe parameters.
func New(logger svc1log.Logger, origin string, level int) logr.Logger {
logger = svc1log.WithParams(logger, svc1log.Origin(origin))
return &svc1logr{
origin: origin,
logger: logger,
level: level,
enabled: true,
}
}

func (s *svc1logr) Info(msg string, keysAndValues ...interface{}) {
if s.Enabled() {
s.logger.Info(msg, toSafeParams(s.logger, keysAndValues))
}
}

func (s *svc1logr) Enabled() bool {
return s.enabled
}

func (s *svc1logr) Error(err error, msg string, keysAndValues ...interface{}) {
s.logger.Error(msg, svc1log.Stacktrace(err), toSafeParams(s.logger, keysAndValues))
}

func (s *svc1logr) V(level int) logr.Logger {
enabled := true
if level > s.level {
enabled = false
}
return &svc1logr{
origin: s.origin,
logger: s.logger,
level: s.level,
enabled: enabled,
}
}

func (s *svc1logr) WithValues(keysAndValues ...interface{}) logr.Logger {
logger := svc1log.WithParams(s.logger, toSafeParams(s.logger, keysAndValues))
return New(logger, s.origin, s.level)
}

func (s *svc1logr) WithName(name string) logr.Logger {
return New(s.logger, path.Join(s.origin, name), s.level)
}

func toSafeParams(logger svc1log.Logger, keysAndValues []interface{}) svc1log.Param {
if len(keysAndValues)%2 == 1 {
logger.Error("Logging error: keysAndValues pair slice has an odd number of arguments; ignoring all",
svc1log.SafeParam("keysAndValuesLen", len(keysAndValues)))
return svc1log.SafeParams(map[string]interface{}{})
}

params := make(map[string]interface{}, len(keysAndValues)/2)
for i := 0; i < len(keysAndValues); i = i + 2 {
key, ok := keysAndValues[i].(string)
if !ok {
logger.Error("Logging error: Key type is not string",
svc1log.SafeParam("actualType", fmt.Sprintf("%T", keysAndValues[i])),
svc1log.SafeParam("key", key))
continue
}
params[key] = keysAndValues[i+1]
}
return svc1log.SafeParams(params)
}
84 changes: 84 additions & 0 deletions adapters/svc1logr/svc1logr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2021 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 svc1logr

import (
"bytes"
"encoding/json"
"fmt"
"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"

// Use zap as logger implementation
_ "github.com/palantir/witchcraft-go-logging/wlog-zap"
)

func TestSvc1LogrWrapper(t *testing.T) {
buf := new(bytes.Buffer)
logger := svc1log.New(buf, wlog.DebugLevel)

logr1 := New(logger, "foo", 1)
logr2 := logr1.WithValues("key2", "val2", "key3", "val3")
logr3 := logr1.WithName("bar")

logr1.Info("logr 1", "key1", "val1")
assertLogLine(t, buf.Bytes(), objmatcher.MapMatcher{
"level": objmatcher.NewEqualsMatcher("INFO"),
"time": objmatcher.NewRegExpMatcher(".+"),
"message": objmatcher.NewEqualsMatcher("logr 1"),
"type": objmatcher.NewEqualsMatcher(svc1log.TypeValue),
"origin": objmatcher.NewEqualsMatcher("foo"),
"params": objmatcher.MapMatcher{
"key1": objmatcher.NewEqualsMatcher("val1"),
},
})
buf.Reset()

logr2.Info("logr 2")
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.NewEqualsMatcher("foo"),
"params": objmatcher.MapMatcher{
"key2": objmatcher.NewEqualsMatcher("val2"),
"key3": objmatcher.NewEqualsMatcher("val3"),
},
})
buf.Reset()

logr3.Error(fmt.Errorf("test error"), "logr 3")
assertLogLine(t, buf.Bytes(), objmatcher.MapMatcher{
"level": objmatcher.NewEqualsMatcher("ERROR"),
"time": objmatcher.NewRegExpMatcher(".+"),
"message": objmatcher.NewEqualsMatcher("logr 3"),
"type": objmatcher.NewEqualsMatcher(svc1log.TypeValue),
"origin": objmatcher.NewEqualsMatcher("foo/bar"),
"stacktrace": objmatcher.NewEqualsMatcher("test error"),
})
buf.Reset()
}

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))
}
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-114.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: improvement
improvement:
description: Add gologr svclog wrapper
links:
- https://github.com/palantir/witchcraft-go-logging/pull/114

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/palantir/witchcraft-go-logging
go 1.13

require (
github.com/go-logr/logr v0.3.0
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/gorilla/mux v1.7.3 // indirect
github.com/nmiyake/pkg/dirs v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/envoyproxy/go-control-plane v0.6.9/go.mod h1:SBwIajubJHhxtWwsL9s8ss4safvEdbitLhGGK48rN6g=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-logr/logr v0.3.0 h1:q4c+kbcR0d5rSurhBR8dIgieOaYpXtsdTYfx22Cu6rs=
github.com/go-logr/logr v0.3.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s=
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
Expand Down