From aa8f498f8e54b6593aac5d9e15c6619f65f0f921 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Wed, 13 Sep 2017 10:23:19 -0700 Subject: [PATCH] Move zap support to subpackage (#13) Depending on bark shouldn't impose a dependency on zap. This moves zap support into a zbark subpackage. --- Makefile | 2 +- interface.go | 6 ------ zbark/doc.go | 20 +++++++++++++++++++ zap_logger.go => zbark/zap_logger.go | 17 +++++++++------- .../zap_logger_test.go | 7 ++++--- 5 files changed, 35 insertions(+), 17 deletions(-) create mode 100644 zbark/doc.go rename zap_logger.go => zbark/zap_logger.go (84%) rename zap_logger_test.go => zbark/zap_logger_test.go (96%) diff --git a/Makefile b/Makefile index 8f49841..ccbca95 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/interface.go b/interface.go index 856828a..abc2cbd 100644 --- a/interface.go +++ b/interface.go @@ -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. @@ -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 diff --git a/zbark/doc.go b/zbark/doc.go new file mode 100644 index 0000000..b437e09 --- /dev/null +++ b/zbark/doc.go @@ -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 diff --git a/zap_logger.go b/zbark/zap_logger.go similarity index 84% rename from zap_logger.go rename to zbark/zap_logger.go index c2e571e..48b34e4 100644 --- a/zap_logger.go +++ b/zbark/zap_logger.go @@ -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 @@ -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. @@ -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 } diff --git a/zap_logger_test.go b/zbark/zap_logger_test.go similarity index 96% rename from zap_logger_test.go rename to zbark/zap_logger_test.go index 3f9a6fb..868d9c4 100644 --- a/zap_logger_test.go +++ b/zbark/zap_logger_test.go @@ -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 @@ -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" @@ -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) {