Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make levels API more composable. #157

Merged
merged 1 commit into from Dec 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 15 additions & 15 deletions log/levels/levels.go
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
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