Skip to content

Commit

Permalink
Merge pull request #12 from jkanywhere/zap
Browse files Browse the repository at this point in the history
Construct a bark logger from a zap logger.
  • Loading branch information
jkanywhere authored Sep 12, 2017
2 parents dbf558e + a68ee02 commit 2cdaf18
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ language: go
sudo: false

go:
- 1.6
- 1.7
- 1.8
- 1.9
- tip

install:
Expand Down
6 changes: 6 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/cactus/go-statsd-client/statsd"
"github.com/sirupsen/logrus"
"go.uber.org/zap"
)

// Logger is an interface for loggers accepted by Uber's libraries.
Expand Down Expand Up @@ -106,6 +107,11 @@ func NewLoggerFromLogrus(logger *logrus.Logger) Logger {
return newBarkLogrusLogger(logger)
}

// NewLoggerFromZap create a bark-compliant wrapper for a zap-brand logger.
func NewLoggerFromZap(logger *zap.Logger) Logger {
return newBarkZapLogger(logger)
}

// Tags is an alias of map[string]string, a type for tags associated with a statistic
type Tags map[string]string

Expand Down
65 changes: 65 additions & 0 deletions zap_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package bark

import (
"sort"

"go.uber.org/zap"
)

func newBarkZapLogger(l *zap.Logger) Logger {
return barkZapLogger{l.Sugar()}
}

type barkZapLogger struct{ *zap.SugaredLogger }

func (l barkZapLogger) WithField(key string, value interface{}) Logger {
l.SugaredLogger = l.SugaredLogger.With(key, value) // safe to change because we pass-by-value
return l
}

func (l barkZapLogger) WithFields(keyValues LogFields) Logger {
barkFields := keyValues.Fields()

// Deterministic ordering of fields.
keys := make([]string, 0, len(barkFields))
for k := range barkFields {
keys = append(keys, k)
}
sort.Strings(keys)

zapFields := make([]interface{}, 0, len(barkFields))
for _, k := range keys {
zapFields = append(zapFields, zap.Any(k, barkFields[k]))
}

l.SugaredLogger = l.SugaredLogger.With(zapFields...) // safe to change because we pass-by-value
return l
}

func (l barkZapLogger) WithError(err error) Logger {
l.SugaredLogger = l.SugaredLogger.With(zap.Error(err)) // safe to change because we pass-by-value
return l
}

func (l barkZapLogger) Fields() Fields {
l.SugaredLogger.Warn("Fields() call to bark logger is not supported by Zap")
return nil
}
117 changes: 117 additions & 0 deletions zap_logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package bark_test

import (
"errors"
"testing"

"github.com/uber-common/bark"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)

func newTestLogger() (bark.Logger, *observer.ObservedLogs) {
core, logs := observer.New(zap.LevelEnablerFunc(func(zapcore.Level) bool {
return true
}))
return bark.NewLoggerFromZap(zap.New(core)), logs
}

func TestBarkLoggerWith(t *testing.T) {
t.Run("WithField", func(t *testing.T) {
l, logs := newTestLogger()
l = l.WithField("foo", "bar")

l.Info("hello")
require.Equal(t, 1, logs.Len(), "message count did not match")

entry := logs.All()[0]
require.Len(t, entry.Context, 1, "context size did not match")
field := entry.Context[0]

assert.Equal(t, zapcore.StringType, field.Type, "field type did not match")
assert.Equal(t, "foo", field.Key, "field name did not match")
assert.Equal(t, "bar", field.String, "field value did not match")
})

t.Run("WithFields", func(t *testing.T) {
l, logs := newTestLogger()
l = l.WithFields(bark.Fields{
"foo": "bar",
"baz": int8(42),
"qux": errors.New("great sadness"),
})

l.Info("hello")
require.Equal(t, 1, logs.Len(), "message count did not match")

entry := logs.All()[0]
require.Len(t, entry.Context, 3, "context size did not match")

// We ensure deterministic ordering of fields by sorting them.
foo, baz, qux := entry.Context[1], entry.Context[0], entry.Context[2]

assert.Equal(t, "foo", foo.Key, "field name did not match")
assert.Equal(t, zapcore.StringType, foo.Type, "field type did not match")
assert.Equal(t, "bar", foo.String, "field value did not match")

assert.Equal(t, "baz", baz.Key, "field name did not match")
assert.Equal(t, zapcore.Int8Type, baz.Type, "field type did not match")
assert.EqualValues(t, 42, baz.Integer, "field value did not match")

assert.Equal(t, "qux", qux.Key, "field name did not match")
assert.Equal(t, zapcore.ErrorType, qux.Type, "field type did not match")
assert.Equal(t, errors.New("great sadness"), qux.Interface, "field value did not match")
})

t.Run("WithError", func(t *testing.T) {
l, logs := newTestLogger()
l = l.WithError(errors.New("great sadness"))

l.Info("hello")
require.Equal(t, 1, logs.Len(), "message count did not match")

entry := logs.All()[0]
require.Len(t, entry.Context, 1, "context size did not match")
field := entry.Context[0]

assert.Equal(t, zapcore.ErrorType, field.Type, "field type did not match")
assert.Equal(t, "error", field.Key, "field name did not match")
assert.Equal(t, errors.New("great sadness"), field.Interface, "field value did not match")
})

t.Run("Fields", func(t *testing.T) {
l, logs := newTestLogger()
l = l.WithField("foo", "bar").WithError(errors.New("great sadness"))
assert.Empty(t, l.Fields(), "expected empty field list")

require.Equal(t, 1, logs.Len(), "message count did not match")

entry := logs.All()[0]
assert.Equal(t,
"Fields() call to bark logger is not supported by Zap", entry.Message,
"message did not match")
assert.Equal(t, zapcore.WarnLevel, entry.Level, "message level did not match")
})
}

0 comments on commit 2cdaf18

Please sign in to comment.