Skip to content

Commit

Permalink
Move zap support to subpackage (#13)
Browse files Browse the repository at this point in the history
Depending on bark shouldn't impose a dependency on zap.

This moves zap support into a zbark subpackage.
  • Loading branch information
abhinav authored Sep 13, 2017
1 parent 2cdaf18 commit aa8f498
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: test
test:
go build -o testhelp/fatal testhelp/fatal.go
go test -v
go test -v . ./zbark

.PHONY: get-deps
get-deps:
Expand Down
6 changes: 0 additions & 6 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ 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 @@ -107,11 +106,6 @@ 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
20 changes: 20 additions & 0 deletions zbark/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2017 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 zbark integrates Bark with Zap (go.uber.org/zap).
package zbark
17 changes: 10 additions & 7 deletions zap_logger.go → zbark/zap_logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015 Uber Technologies, Inc.
// Copyright (c) 2017 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
Expand All @@ -16,26 +16,29 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package bark
package zbark

import (
"sort"

"github.com/uber-common/bark"

"go.uber.org/zap"
)

func newBarkZapLogger(l *zap.Logger) Logger {
// New builds a Bark logger from a Zap logger.
func New(l *zap.Logger) bark.Logger {
return barkZapLogger{l.Sugar()}
}

type barkZapLogger struct{ *zap.SugaredLogger }

func (l barkZapLogger) WithField(key string, value interface{}) Logger {
func (l barkZapLogger) WithField(key string, value interface{}) bark.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 {
func (l barkZapLogger) WithFields(keyValues bark.LogFields) bark.Logger {
barkFields := keyValues.Fields()

// Deterministic ordering of fields.
Expand All @@ -54,12 +57,12 @@ func (l barkZapLogger) WithFields(keyValues LogFields) Logger {
return l
}

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

func (l barkZapLogger) Fields() Fields {
func (l barkZapLogger) Fields() bark.Fields {
l.SugaredLogger.Warn("Fields() call to bark logger is not supported by Zap")
return nil
}
7 changes: 4 additions & 3 deletions zap_logger_test.go → zbark/zap_logger_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015 Uber Technologies, Inc.
// Copyright (c) 2017 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
Expand All @@ -16,13 +16,14 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package bark_test
package zbark_test

import (
"errors"
"testing"

"github.com/uber-common/bark"
"github.com/uber-common/bark/zbark"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -35,7 +36,7 @@ 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
return zbark.New(zap.New(core)), logs
}

func TestBarkLoggerWith(t *testing.T) {
Expand Down

0 comments on commit aa8f498

Please sign in to comment.