Skip to content

Commit

Permalink
Combine config and Levels structs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisHines committed Jul 14, 2015
1 parent 8899b82 commit 803da74
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions log/levels/levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,107 +7,107 @@ import "github.com/go-kit/kit/log"
// want a different set of levels, you can create your own levels type very
// easily, and you can elide the configuration.
type Levels struct {
ctx log.Context
cfg *config
ctx log.Context
levelKey string

debugValue string
infoValue string
warnValue string
errorValue string
critValue string
}

// New creates a new leveled logger, wrapping the passed logger.
func New(logger log.Logger, options ...Option) Levels {
cfg := &config{
levelKey: "level",
l := Levels{
ctx: log.NewContext(logger),
levelKey: "level",

debugValue: "debug",
infoValue: "info",
warnValue: "warn",
errorValue: "error",
critValue: "crit",
}
for _, option := range options {
option(cfg)
}
return Levels{
ctx: log.NewContext(logger),
cfg: cfg,
option(&l)
}
return l
}

// With returns a new leveled logger that includes keyvals in all log events.
func (l Levels) With(keyvals ...interface{}) Levels {
return Levels{
ctx: l.ctx.With(keyvals...),
cfg: l.cfg,
ctx: l.ctx.With(keyvals...),
levelKey: l.levelKey,
debugValue: l.debugValue,
infoValue: l.infoValue,
warnValue: l.warnValue,
errorValue: l.errorValue,
critValue: l.critValue,
}
}

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

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

// Warn logs a warn event along with keyvals.
func (l Levels) Warn(keyvals ...interface{}) error {
return l.ctx.WithPrefix(l.cfg.levelKey, l.cfg.warnValue).Log(keyvals...)
return l.ctx.WithPrefix(l.levelKey, l.warnValue).Log(keyvals...)
}

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

// Crit logs a crit event along with keyvals.
func (l Levels) Crit(keyvals ...interface{}) error {
return l.ctx.WithPrefix(l.cfg.levelKey, l.cfg.critValue).Log(keyvals...)
}

type config struct {
levelKey string

debugValue string
infoValue string
warnValue string
errorValue string
critValue string
return l.ctx.WithPrefix(l.levelKey, l.critValue).Log(keyvals...)
}

// Option sets a parameter for leveled loggers.
type Option func(*config)
type Option func(*Levels)

// Key sets the key for the field used to indicate log level. By default,
// the key is "level".
func Key(key string) Option {
return func(c *config) { c.levelKey = key }
return func(l *Levels) { l.levelKey = key }
}

// DebugValue sets the value for the field used to indicate the debug log
// level. By default, the value is "debug".
func DebugValue(value string) Option {
return func(c *config) { c.debugValue = value }
return func(l *Levels) { l.debugValue = value }
}

// InfoValue sets the value for the field used to indicate the info log level.
// By default, the value is "info".
func InfoValue(value string) Option {
return func(c *config) { c.infoValue = value }
return func(l *Levels) { l.infoValue = value }
}

// WarnValue sets the value for the field used to indicate the warning log
// level. By default, the value is "warn".
func WarnValue(value string) Option {
return func(c *config) { c.warnValue = value }
return func(l *Levels) { l.warnValue = value }
}

// ErrorValue sets the value for the field used to indicate the error log
// level. By default, the value is "error".
func ErrorValue(value string) Option {
return func(c *config) { c.errorValue = value }
return func(l *Levels) { l.errorValue = value }
}

// CritValue sets the value for the field used to indicate the critical log
// level. By default, the value is "crit".
func CritValue(value string) Option {
return func(c *config) { c.critValue = value }
return func(l *Levels) { l.critValue = value }
}

0 comments on commit 803da74

Please sign in to comment.