Skip to content

Instantly share code, notes, and snippets.

@AlmogBaku
Last active May 18, 2016 23:23
Show Gist options
  • Save AlmogBaku/61f183350e10173377f85ee940a84741 to your computer and use it in GitHub Desktop.
Save AlmogBaku/61f183350e10173377f85ee940a84741 to your computer and use it in GitHub Desktop.
LeveledLogger
package leveledlogger
import (
"github.com/go-kit/kit/log"
)
type LogLevel int
const (
Off LogLevel = iota
Fatal
Error
Warn
Info
Debug
)
func (l LogLevel) String() string {
switch l {
case Fatal:
return "fatal"
case Error:
return "error"
case Warn:
return "warning"
case Info:
return "info"
case Debug:
return "debug"
default:
return "unknown"
}
}
type leveledLogger struct {
o log.Logger
level LogLevel
}
func NewLeveledLog(logger log.Logger, level LogLevel) log.Logger {
return &leveledLogger{logger, level}
}
func (l leveledLogger) Log(keyvals ...interface{}) error {
if l.level == Off {
return
}
len := len(keyvals)
for i := 0; i < len; i += 2 {
if (i+1) < len && keyvals[i] == "level" {
if lvl, ok := keyvals[i+1].(LogLevel); ok && lvl <= l.level {
return l.o.Log(keyvals...)
}
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment