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

kit/log/levels: Add enabled level #322

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions log/levels/enabled/enabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package enabled

import (
"errors"

"github.com/go-kit/kit/log/levels"
)

// LevelEnabledLogger provides a wrapper around a logger with a feature
// for checking a level is enabled. It has five
// levels: debug, info, warning (warn), error, and critical (crit).
//
// It is users' responsibility not to call the Log method of a logger.
type LevelEnabledLogger struct {
levels.Levels
debugEnabled bool
infoEnabled bool
warnEnabled bool
errorEnabled bool
critEnabled bool
}

// New creates a new level enabled logger, wrapping the passed logger.
// level must be one of debug, info, warning (warn), error, critical (crit)
// or an empty string (which means info).
// It returns an error only when the level value is invalid.
func New(logger levels.Levels, level string) (LevelEnabledLogger, error) {
switch level {
case "debug":
return NewDebug(logger), nil
case "", "info":
return NewInfo(logger), nil
case "warn", "warning":
return NewWarn(logger), nil
case "error":
return NewError(logger), nil
case "crit", "critical":
return NewCrit(logger), nil
default:
return LevelEnabledLogger{}, errors.New("invalid log level")
}
}

// NewDebug creates a new debug level enabled logger, wrapping the passed logger.
func NewDebug(logger levels.Levels) LevelEnabledLogger {
return LevelEnabledLogger{
Levels: logger,
debugEnabled: true,
infoEnabled: true,
warnEnabled: true,
errorEnabled: true,
critEnabled: true,
}
}

// NewInfo creates a new info level enabled logger, wrapping the passed logger.
func NewInfo(logger levels.Levels) LevelEnabledLogger {
return LevelEnabledLogger{
Levels: logger,
infoEnabled: true,
warnEnabled: true,
errorEnabled: true,
critEnabled: true,
}
}

// NewWarn creates a new warn level enabled logger, wrapping the passed logger.
func NewWarn(logger levels.Levels) LevelEnabledLogger {
return LevelEnabledLogger{
Levels: logger,
warnEnabled: true,
errorEnabled: true,
critEnabled: true,
}
}

// NewError creates a new error level enabled logger, wrapping the passed logger.
func NewError(logger levels.Levels) LevelEnabledLogger {
return LevelEnabledLogger{
Levels: logger,
errorEnabled: true,
critEnabled: true,
}
}

// NewCrit creates a new crit level enabled logger, wrapping the passed logger.
func NewCrit(logger levels.Levels) LevelEnabledLogger {
return LevelEnabledLogger{
Levels: logger,
errorEnabled: true,
critEnabled: true,
}
}

// DebugEnabled returns the debug level is enabled
func (l LevelEnabledLogger) DebugEnabled() bool { return l.debugEnabled }

// InfoEnabled returns the info level is enabled
func (l LevelEnabledLogger) InfoEnabled() bool { return l.infoEnabled }

// WarnEnabled returns the warn level is enabled
func (l LevelEnabledLogger) WarnEnabled() bool { return l.warnEnabled }

// ErrorEnabled returns the error level is enabled
func (l LevelEnabledLogger) ErrorEnabled() bool { return l.errorEnabled }

// CritEnabled returns the crit level is enabled
func (l LevelEnabledLogger) CritEnabled() bool { return l.critEnabled }
67 changes: 67 additions & 0 deletions log/levels/enabled/enabled_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package enabled_test

import (
"bytes"
"os"
"testing"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/levels"
"github.com/go-kit/kit/log/levels/enabled"
)

func TestInfoLevel(t *testing.T) {
buf := bytes.Buffer{}
logger := enabled.NewInfo(levels.New(log.NewLogfmtLogger(&buf)))

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

buf.Reset()
if logger.InfoEnabled() {
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()
if logger.ErrorEnabled() {
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()
if logger.CritEnabled() {
logger.Crit().Log("msg", " ")
}
if want, have := "level=crit msg=\"\\t\"\n", buf.String(); want != have {
t.Errorf("want %#v, have %#v", want, have)
}
}

func ExampleEnabled() {
logger, err := enabled.New(levels.New(log.NewLogfmtLogger(os.Stdout)), "warn")
if err != nil {
// This happens only when the level is invalid.
// In this example, this never happens as the valid level "warn" is used.
// In a real usage like reading a level from a command line option or a
// config file, you should check this error.
panic(err)
}
if logger.DebugEnabled() {
logger.Debug().Log("msg", "hello")
}
if logger.WarnEnabled() {
logger.With("context", "foo").Warn().Log("err", "error")
}

// Output:
// level=warn context=foo err=error
}