wemo/logger/logger.go

66 lines
1.5 KiB
Go

package logger
import (
"log"
"os"
)
type LoggerInterface interface {
Tracef(fmt string, args ...interface{})
Infof(fmt string, args ...interface{})
Warnf(fmt string, args ...interface{})
Errorf(fmt string, args ...interface{})
}
// a default implementation of the LoggerInterface, simply using the 'log' library
type LeveledLogger struct {
level int
}
var std = LeveledLogger{}
var levels = map[string]int{
"ERROR": 400,
"WARN" : 300,
"INFO" : 200,
"TRACE" : 100,
}
func (l *LeveledLogger) levelPrint(minimumLevel int, fmt string, args ...interface{}) {
if l.level == 0 {
l.level = levels[os.Getenv("LOGLVL")]
if l.level == 0 { l.level = levels["WARN"] }
}
if minimumLevel >= l.level {
log.Printf(fmt+"\n", args...)
}
}
func (l *LeveledLogger) Tracef(fmt string, args ...interface{}) {
l.levelPrint(levels["TRACE"], fmt, args...)
}
func (l *LeveledLogger) Infof(fmt string, args ...interface{}) {
l.levelPrint(levels["INFO"], fmt, args...)
}
func (l *LeveledLogger) Warnf(fmt string, args ...interface{}) {
l.levelPrint(levels["WARN"], fmt, args...)
}
func (l *LeveledLogger) Errorf(fmt string, args ...interface{}) {
l.levelPrint(levels["ERROR"], fmt, args...)
}
func Tracef(fmt string, args ...interface{}) {
std.levelPrint(levels["TRACE"], fmt, args...)
}
func Infof(fmt string, args ...interface{}) {
std.levelPrint(levels["INFO"], fmt, args...)
}
func Warnf(fmt string, args ...interface{}) {
std.levelPrint(levels["WARN"], fmt, args...)
}
func Errorf(fmt string, args ...interface{}) {
std.levelPrint(levels["ERROR"], fmt, args...)
}