Skip to content

Commit

Permalink
Merge aac1295 into 469a128
Browse files Browse the repository at this point in the history
  • Loading branch information
billf committed Jun 30, 2016
2 parents 469a128 + aac1295 commit df27b1c
Show file tree
Hide file tree
Showing 4 changed files with 308 additions and 23 deletions.
44 changes: 22 additions & 22 deletions zbark/bark.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,89 +29,89 @@ import (
"github.com/uber-common/bark"
)

// Barkify wraps zap's JSON logger to make it compatible with the bark.Logger
// Barkify wraps zap's JSON barker to make it compatible with the bark.Logger
// interface.
func Barkify(l zap.Logger) bark.Logger {
return &logger{
return &barker{
zl: l,
fields: make(bark.Fields),
}
}

type logger struct {
type barker struct {
zl zap.Logger
fields bark.Fields
}

func (l *logger) Debug(args ...interface{}) {
func (l *barker) Debug(args ...interface{}) {
l.zl.Debug(fmt.Sprint(args...))
}

func (l *logger) Debugf(format string, args ...interface{}) {
func (l *barker) Debugf(format string, args ...interface{}) {
l.zl.Debug(fmt.Sprintf(format, args...))
}

func (l *logger) Info(args ...interface{}) {
func (l *barker) Info(args ...interface{}) {
l.zl.Info(fmt.Sprint(args...))
}

func (l *logger) Infof(format string, args ...interface{}) {
func (l *barker) Infof(format string, args ...interface{}) {
l.zl.Info(fmt.Sprintf(format, args...))
}

func (l *logger) Warn(args ...interface{}) {
func (l *barker) Warn(args ...interface{}) {
l.zl.Warn(fmt.Sprint(args...))
}

func (l *logger) Warnf(format string, args ...interface{}) {
func (l *barker) Warnf(format string, args ...interface{}) {
l.zl.Warn(fmt.Sprintf(format, args...))
}

func (l *logger) Error(args ...interface{}) {
func (l *barker) Error(args ...interface{}) {
l.zl.Error(fmt.Sprint(args...))
}

func (l *logger) Errorf(format string, args ...interface{}) {
func (l *barker) Errorf(format string, args ...interface{}) {
l.zl.Error(fmt.Sprintf(format, args...))
}

func (l *logger) Panic(args ...interface{}) {
func (l *barker) Panic(args ...interface{}) {
l.zl.Panic(fmt.Sprint(args...))
}

func (l *logger) Panicf(format string, args ...interface{}) {
func (l *barker) Panicf(format string, args ...interface{}) {
l.zl.Panic(fmt.Sprintf(format, args...))
}

func (l *logger) Fatal(args ...interface{}) {
func (l *barker) Fatal(args ...interface{}) {
l.zl.Fatal(fmt.Sprint(args...))
}

func (l *logger) Fatalf(format string, args ...interface{}) {
func (l *barker) Fatalf(format string, args ...interface{}) {
l.zl.Fatal(fmt.Sprintf(format, args...))
}

func (l *logger) WithField(key string, val interface{}) bark.Logger {
func (l *barker) WithField(key string, val interface{}) bark.Logger {
newFields := bark.Fields{key: val}
return &logger{
return &barker{
zl: l.addZapFields(newFields),
fields: l.addBarkFields(newFields),
}
}

func (l *logger) WithFields(fs bark.LogFields) bark.Logger {
func (l *barker) WithFields(fs bark.LogFields) bark.Logger {
newFields := fs.Fields()
return &logger{
return &barker{
zl: l.addZapFields(newFields),
fields: l.addBarkFields(newFields),
}
}

func (l *logger) Fields() bark.Fields {
func (l *barker) Fields() bark.Fields {
return l.fields
}

func (l *logger) addBarkFields(fs bark.Fields) bark.Fields {
func (l *barker) addBarkFields(fs bark.Fields) bark.Fields {
newFields := make(bark.Fields, len(l.fields)+len(fs))
for k, v := range l.fields {
newFields[k] = v
Expand All @@ -122,7 +122,7 @@ func (l *logger) addBarkFields(fs bark.Fields) bark.Fields {
return newFields
}

func (l *logger) addZapFields(fs bark.Fields) zap.Logger {
func (l *barker) addZapFields(fs bark.Fields) zap.Logger {
zfs := make([]zap.Field, 0, len(fs))
for key, val := range fs {
switch v := val.(type) {
Expand Down
144 changes: 144 additions & 0 deletions zbark/debark.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright (c) 2016 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

import (
"github.com/uber-go/zap"
"github.com/uber-go/zap/zwrap"

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

// Debarkify wraps bark.Logger to make it compatible with zap's JSON logger
func Debarkify(b bark.Logger) zap.Logger {
if wrapper, ok := b.(*barker); ok {
return wrapper.zl
}
return &zapper{
lvl: zap.DebugLevel,
bl: b,
}
}

type zapper struct {
lvl zap.Level
bl bark.Logger
}

func (z *zapper) DFatal(msg string, fields ...zap.Field) {
// TODO: Implement development/DFatal
z.Error(msg, fields...)
}

func (z *zapper) Log(l zap.Level, msg string, fields ...zap.Field) {
if l < z.lvl {
return
}
switch l {
case zap.DebugLevel:
z.Debug(msg, fields...)
case zap.InfoLevel:
z.Info(msg, fields...)
case zap.WarnLevel:
z.Warn(msg, fields...)
case zap.ErrorLevel:
z.Error(msg, fields...)
case zap.PanicLevel:
z.Panic(msg, fields...)
case zap.FatalLevel:
z.Fatal(msg, fields...)
default:
panic("passed an unknown zap.Level")
}
}

func (z *zapper) Level() zap.Level {
return z.lvl
}

// Change the level of this logger, as well as all its ancestors and
// descendants. This makes it easy to change the log level at runtime
// without restarting your application.
func (z *zapper) SetLevel(l zap.Level) {
z.lvl = l
}

// Create a child logger, and optionally add some context to that logger.
func (z *zapper) With(fields ...zap.Field) zap.Logger {
return &zapper{
lvl: z.lvl,
bl: z.bl.WithFields(zapToBark(fields)),
}
}

func (z *zapper) StubTime() {
// StubTime stops the logger from including the current time in each
// message. Instead, it always reports the time as Unix epoch 0. (This is
// useful in tests and examples.)
panic("fake implementation")
}

func (z *zapper) Check(l zap.Level, msg string) *zap.CheckedMessage {
if l < z.lvl {
return nil
}
return zap.NewCheckedMessage(z, l, msg)
}

type zapperBarkFields struct {
zwrap.KeyValueMap
}

func (zbf zapperBarkFields) Fields() map[string]interface{} {
return zbf.KeyValueMap
}

func zapToBark(zfs []zap.Field) bark.LogFields {
zbf := zapperBarkFields{make(zwrap.KeyValueMap, len(zfs))}
for _, zf := range zfs {
_ = zf.AddTo(zbf)
}
return zbf
}

func (z *zapper) Debug(msg string, fields ...zap.Field) {
z.bl.WithFields(zapToBark(fields)).Debug(msg)
}

func (z *zapper) Info(msg string, fields ...zap.Field) {
z.bl.WithFields(zapToBark(fields)).Info(msg)
}

func (z *zapper) Warn(msg string, fields ...zap.Field) {
z.bl.WithFields(zapToBark(fields)).Warn(msg)
}

func (z *zapper) Error(msg string, fields ...zap.Field) {
z.bl.WithFields(zapToBark(fields)).Error(msg)
}

func (z *zapper) Panic(msg string, fields ...zap.Field) {
z.bl.WithFields(zapToBark(fields)).Panic(msg)
}

func (z *zapper) Fatal(msg string, fields ...zap.Field) {
z.bl.WithFields(zapToBark(fields)).Fatal(msg)
}
Loading

0 comments on commit df27b1c

Please sign in to comment.