Skip to content

Commit

Permalink
Merge pull request go-kit#157 from go-kit/levels-api
Browse files Browse the repository at this point in the history
Make levels API more composable.
  • Loading branch information
ChrisHines committed Dec 6, 2015
2 parents 3bd5969 + 3987fd0 commit 9d15fd6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
30 changes: 15 additions & 15 deletions log/levels/levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,29 @@ func (l Levels) With(keyvals ...interface{}) Levels {
}
}

// Debug logs a debug event along with keyvals.
func (l Levels) Debug(keyvals ...interface{}) error {
return l.ctx.WithPrefix(l.levelKey, l.debugValue).Log(keyvals...)
// Debug returns a debug level logger.
func (l Levels) Debug(keyvals ...interface{}) log.Logger {
return l.ctx.WithPrefix(l.levelKey, l.debugValue)
}

// Info logs an info event along with keyvals.
func (l Levels) Info(keyvals ...interface{}) error {
return l.ctx.WithPrefix(l.levelKey, l.infoValue).Log(keyvals...)
// Info returns an info level logger.
func (l Levels) Info(keyvals ...interface{}) log.Logger {
return l.ctx.WithPrefix(l.levelKey, l.infoValue)
}

// Warn logs a warn event along with keyvals.
func (l Levels) Warn(keyvals ...interface{}) error {
return l.ctx.WithPrefix(l.levelKey, l.warnValue).Log(keyvals...)
// Warn returns a warning level logger.
func (l Levels) Warn(keyvals ...interface{}) log.Logger {
return l.ctx.WithPrefix(l.levelKey, l.warnValue)
}

// Error logs an error event along with keyvals.
func (l Levels) Error(keyvals ...interface{}) error {
return l.ctx.WithPrefix(l.levelKey, l.errorValue).Log(keyvals...)
// Error returns an error level logger.
func (l Levels) Error(keyvals ...interface{}) log.Logger {
return l.ctx.WithPrefix(l.levelKey, l.errorValue)
}

// Crit logs a crit event along with keyvals.
func (l Levels) Crit(keyvals ...interface{}) error {
return l.ctx.WithPrefix(l.levelKey, l.critValue).Log(keyvals...)
// Crit returns a critical level logger.
func (l Levels) Crit(keyvals ...interface{}) log.Logger {
return l.ctx.WithPrefix(l.levelKey, l.critValue)
}

// Option sets a parameter for leveled loggers.
Expand Down
14 changes: 7 additions & 7 deletions log/levels/levels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ func TestDefaultLevels(t *testing.T) {
buf := bytes.Buffer{}
logger := levels.New(log.NewLogfmtLogger(&buf))

logger.Debug("msg", "résumé") // of course you'd want to do this
logger.Debug().Log("msg", "résumé") // of course you'd want to do this
if want, have := "level=debug msg=résumé\n", buf.String(); want != have {
t.Errorf("want %#v, have %#v", want, have)
}

buf.Reset()
logger.Info("msg", "Åhus")
logger.Info().Log("msg", "Åhus")
if want, have := "level=info msg=Åhus\n", buf.String(); want != have {
t.Errorf("want %#v, have %#v", want, have)
}

buf.Reset()
logger.Error("msg", "© violation")
logger.Error().Log("msg", "© violation")
if want, have := "level=error msg=\"© violation\"\n", buf.String(); want != have {
t.Errorf("want %#v, have %#v", want, have)
}

buf.Reset()
logger.Crit("msg", " ")
logger.Crit().Log("msg", " ")
if want, have := "level=crit msg=\"\\t\"\n", buf.String(); want != have {
t.Errorf("want %#v, have %#v", want, have)
}
Expand All @@ -48,16 +48,16 @@ func TestModifiedLevels(t *testing.T) {
levels.ErrorValue("err"),
levels.CritValue("crt"),
)
logger.With("easter_island", "176°").Debug("msg", "moai")
logger.With("easter_island", "176°").Debug().Log("msg", "moai")
if want, have := `{"easter_island":"176°","l":"dbg","msg":"moai"}`+"\n", buf.String(); want != have {
t.Errorf("want %#v, have %#v", want, have)
}
}

func ExampleLevels() {
logger := levels.New(log.NewLogfmtLogger(os.Stdout))
logger.Debug("msg", "hello")
logger.With("context", "foo").Warn("err", "error")
logger.Debug().Log("msg", "hello")
logger.With("context", "foo").Warn().Log("err", "error")

// Output:
// level=debug msg=hello
Expand Down

0 comments on commit 9d15fd6

Please sign in to comment.