Skip to content

Commit

Permalink
Logger: drop .Log
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua T Corbin committed Dec 13, 2016
1 parent 92daeb6 commit 76e507a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 49 deletions.
1 change: 0 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ type Logger interface {
// process, but calling Log(PanicLevel, ...) or Log(FatalLevel, ...) should
// not. It may not be possible for compatibility wrappers to comply with
// this last part (e.g. the bark wrapper).
Log(Level, string, ...Field)
Debug(string, ...Field)
Info(string, ...Field)
Warn(string, ...Field)
Expand Down
9 changes: 0 additions & 9 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,12 @@ func TestJSONLoggerWith(t *testing.T) {
})
}

func TestJSONLoggerLog(t *testing.T) {
withJSONLogger(t, DebugLevel, nil, func(logger Logger, buf *testBuffer) {
logger.Log(DebugLevel, "foo")
assert.Equal(t, `{"level":"debug","msg":"foo"}`, buf.Stripped(), "Unexpected output from Log.")
})
}

func TestJSONLoggerLogPanic(t *testing.T) {
for _, tc := range []struct {
do func(Logger)
should bool
expected string
}{
{func(logger Logger) { logger.Log(PanicLevel, "foo") }, false, `{"level":"panic","msg":"foo"}`},
{func(logger Logger) { logger.Check(PanicLevel, "bar").Write() }, true, `{"level":"panic","msg":"bar"}`},
{func(logger Logger) { logger.Panic("baz") }, true, `{"level":"panic","msg":"baz"}`},
} {
Expand All @@ -214,7 +206,6 @@ func TestJSONLoggerLogFatal(t *testing.T) {
status int
expected string
}{
{func(logger Logger) { logger.Log(FatalLevel, "foo") }, false, 0, `{"level":"fatal","msg":"foo"}`},
{func(logger Logger) { logger.Check(FatalLevel, "bar").Write() }, true, 1, `{"level":"fatal","msg":"bar"}`},
{func(logger Logger) { logger.Fatal("baz") }, true, 1, `{"level":"fatal","msg":"baz"}`},
} {
Expand Down
29 changes: 0 additions & 29 deletions tee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,12 @@ func TestTeeLogsBoth(t *testing.T) {
fac2, sink2 := spy.New(zap.WarnLevel)
log := zap.New(zap.Tee(fac1, fac2))

log.Log(zap.InfoLevel, "log @info")
log.Log(zap.WarnLevel, "log @warn")

log.Debug("log-dot-debug")
log.Info("log-dot-info")
log.Warn("log-dot-warn")
log.Error("log-dot-error")

assert.Equal(t, []spy.Log{
{
Level: zap.InfoLevel,
Msg: "log @info",
Fields: []zap.Field{},
},
{
Level: zap.WarnLevel,
Msg: "log @warn",
Fields: []zap.Field{},
},
{
Level: zap.DebugLevel,
Msg: "log-dot-debug",
Expand All @@ -76,11 +63,6 @@ func TestTeeLogsBoth(t *testing.T) {
}, sink1.Logs())

assert.Equal(t, []spy.Log{
{
Level: zap.WarnLevel,
Msg: "log @warn",
Fields: []zap.Field{},
},
{
Level: zap.WarnLevel,
Msg: "log-dot-warn",
Expand All @@ -101,7 +83,6 @@ func TestTee_Panic(t *testing.T) {

assert.Panics(t, func() { log.Panic("foo") }, "tee logger.Panic panics")
assert.Panics(t, func() { log.Check(zap.PanicLevel, "bar").Write() }, "tee logger.Check(PanicLevel).Write() panics")
assert.NotPanics(t, func() { log.Log(zap.PanicLevel, "baz") }, "tee logger.Log(PanicLevel) does not panic")

assert.Equal(t, []spy.Log{
{
Expand All @@ -114,11 +95,6 @@ func TestTee_Panic(t *testing.T) {
Msg: "bar",
Fields: []zap.Field{},
},
{
Level: zap.PanicLevel,
Msg: "baz",
Fields: []zap.Field{},
},
}, sink1.Logs())

assert.Equal(t, []spy.Log{
Expand All @@ -132,11 +108,6 @@ func TestTee_Panic(t *testing.T) {
Msg: "bar",
Fields: []zap.Field{},
},
{
Level: zap.PanicLevel,
Msg: "baz",
Fields: []zap.Field{},
},
}, sink2.Logs())
}

Expand Down
2 changes: 1 addition & 1 deletion text_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func withTextLogger(t testing.TB, enab LevelEnabler, opts []Option, f func(Logge

func TestTextLoggerDebugLevel(t *testing.T) {
withTextLogger(t, DebugLevel, nil, func(logger Logger, buf *testBuffer) {
logger.Log(DebugLevel, "foo")
logger.Debug("foo")
assert.Equal(t, "[D] foo", buf.Stripped(), "Unexpected output from logger")
})
}
Expand Down
38 changes: 30 additions & 8 deletions zbark/debark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package zbark

import (
"bytes"
"fmt"
"testing"

"go.uber.org/zap"
Expand Down Expand Up @@ -92,6 +93,27 @@ var levels = []zap.Level{
zap.ErrorLevel,
}

func logAt(logger zap.Logger, lvl zap.Level, msg string, fields ...zap.Field) {
switch lvl {
case zap.DebugLevel:
logger.Debug(msg, fields...)
case zap.InfoLevel:
logger.Info(msg, fields...)
case zap.WarnLevel:
logger.Warn(msg, fields...)
case zap.ErrorLevel:
logger.Error(msg, fields...)
case zap.DPanicLevel:
logger.DPanic(msg, fields...)
case zap.PanicLevel:
logger.Panic(msg, fields...)
case zap.FatalLevel:
logger.Fatal(msg, fields...)
default:
panic(fmt.Sprintf("unknown log level %v", lvl))
}
}

func TestDebark_Check(t *testing.T) {
logger, buf := newDebark(zap.DebugLevel)
for _, l := range append(levels, zap.PanicLevel, zap.FatalLevel) {
Expand Down Expand Up @@ -123,29 +145,29 @@ func TestDebark_Check(t *testing.T) {
})
}

// TODO: test this behavior implemented by the barkFacility
// assert.NotPanics(t, func() { bf.Log(zap.Level(31337), "") })
// N.B. logs an internal error to ErrorOutput (currently inaccessible under Debarkify):
// 2016-12-13 19:23:28.943923799 +0000 UTC facility error: unable to map zap.Level Level(31337) to bark

func TestDebark_LeveledLogging(t *testing.T) {
logger, buf := newDebark(zap.DebugLevel)
for _, l := range levels {
require.Equal(t, 0, buf.Len(), "buffer not zero")
logger.Log(l, "ohai")
logAt(logger, l, "ohai")
assert.NotEqual(t, 0, buf.Len(), "%q did not log", l)
buf.Reset()
}

logger, buf = newDebark(zap.FatalLevel)
require.Equal(t, 0, buf.Len(), "buffer not zero to begin test")
for _, l := range append(levels) {
logger.Log(l, "ohai")
logAt(logger, l, "ohai")
assert.Equal(t, 0, buf.Len(), "buffer not zero, we should not have logged")
}

logger, buf = newDebark(zap.DebugLevel)

assert.NotPanics(t, func() { logger.Log(zap.Level(31337), "") })
// N.B. logs an internal error to ErrorOutput (currently inaccessible under Debarkify):
// 2016-12-13 19:23:28.943923799 +0000 UTC facility error: unable to map zap.Level Level(31337) to bark

assert.Panics(t, func() { logger.Log(zap.PanicLevel, "") })
assert.Panics(t, func() { logger.Panic("") })
}

func TestDebark_Methods(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion zwrap/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestSampler(t *testing.T) {
},
{
level: zap.ErrorLevel,
logFunc: func(sampler zap.Logger, n int) { WithIter(sampler, n).Log(zap.ErrorLevel, "sample") },
logFunc: func(sampler zap.Logger, n int) { WithIter(sampler, n).Error("sample") },
},
}

Expand Down

0 comments on commit 76e507a

Please sign in to comment.