Repository: uber-go/zap
File: zbark/debark.go (Go)

Code before:
// 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)
}


Review comment (bug):
yeah, i picked `zap.DebugLevel` so that `Log()` would default open and `SetLevel()` could be used to clamp down if desired. i'm not sure what callers expect.


Code after:
// 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 (
	"fmt"

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

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

type zapperBarkFields zwrap.KeyValueMap

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

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) {
	switch l {
	case zap.PanicLevel, zap.FatalLevel:
	default:
		if l < z.lvl {
			return
		}
	}
	bl := z.bl.WithFields(zapToBark(fields))
	switch l {
	case zap.DebugLevel:
		bl.Debug(msg)
	case zap.InfoLevel:
		bl.Info(msg)
	case zap.WarnLevel:
		bl.Warn(msg)
	case zap.ErrorLevel:
		bl.Error(msg)
	case zap.PanicLevel:
		bl.Panic(msg)
	case zap.FatalLevel:
		bl.Fatal(msg)
	default:
		panic(fmt.Errorf("passed an unknown zap.Level: %v", l))
	}
}

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) Check(l zap.Level, msg string) *zap.CheckedMessage {
	switch l {
	case zap.PanicLevel, zap.FatalLevel:
	default:
		if l < z.lvl {
			return nil
		}
	}
	return zap.NewCheckedMessage(z, l, msg)
}

func (z *zapper) Debug(msg string, fields ...zap.Field) {
	z.Log(zap.DebugLevel, msg, fields...)
}

func (z *zapper) Info(msg string, fields ...zap.Field) {
	z.Log(zap.InfoLevel, msg, fields...)
}

func (z *zapper) Warn(msg string, fields ...zap.Field) {
	z.Log(zap.WarnLevel, msg, fields...)
}

func (z *zapper) Error(msg string, fields ...zap.Field) {
	z.Log(zap.ErrorLevel, msg, fields...)
}

func (z *zapper) Panic(msg string, fields ...zap.Field) {
	z.Log(zap.PanicLevel, msg, fields...)
}

func (z *zapper) Fatal(msg string, fields ...zap.Field) {
	z.Log(zap.FatalLevel, msg, fields...)
}

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

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