Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ 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).
[zerolog](https://github.com/rs/zerolog) and [glog](https://github.com/golang/glog). We also provide an implementation
for [go-logr](https://github.com/go-logr/logr) that uses svc1log internally.

Architecture
------------
Expand Down
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
1 change: 1 addition & 0 deletions svc1logr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A [logr](https://github.com/go-logr/logr) implementation using wlog.
83 changes: 83 additions & 0 deletions svc1logr/svc1logr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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/filepath"

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

type svc1logr struct {
origin string
logger svc1log.Logger
}

// New returns a go-logr interface implementation that uses svc1log internally.
func New(logger svc1log.Logger, origin string) logr.Logger {
logger = svc1log.WithParams(logger, svc1log.Origin(origin))
return &svc1logr{
origin: origin,
logger: logger,
}
}

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

func (s *svc1logr) Enabled() bool {
return true
}

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.InfoLogger {
return New(s.logger, s.origin)
}

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

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

func toSafeParams(logger svc1log.Logger, keysAndValues []interface{}) svc1log.Param {
if len(keysAndValues)%2 == 1 {
logger.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("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 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")
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))
}
Loading