2015-07-28 14:04:24 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
2015-07-28 14:09:42 +00:00
|
|
|
// LogLevel defines a type for named log levels
|
2015-07-28 14:04:24 +00:00
|
|
|
type LogLevel uint
|
|
|
|
|
2015-07-28 14:09:42 +00:00
|
|
|
// Pre-Defined log levels to be used with this logging module
|
2015-07-28 14:04:24 +00:00
|
|
|
const (
|
|
|
|
Error LogLevel = iota
|
|
|
|
Warning
|
|
|
|
Info
|
|
|
|
Debug
|
|
|
|
)
|
|
|
|
|
|
|
|
// Logger is a wrapper around output to filter according to levels
|
|
|
|
type Logger struct {
|
|
|
|
Level LogLevel
|
|
|
|
}
|
|
|
|
|
2015-07-28 14:09:42 +00:00
|
|
|
// New instanciates a new Logger and sets the preferred log level
|
2015-07-28 14:04:24 +00:00
|
|
|
func New(logLevel LogLevel) *Logger {
|
|
|
|
return &Logger{
|
|
|
|
Level: logLevel,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 14:09:42 +00:00
|
|
|
// Log is the filtered equivalent to fmt.Println
|
2015-07-28 14:04:24 +00:00
|
|
|
func (l *Logger) Log(level LogLevel, line string) {
|
|
|
|
if l.Level >= level {
|
|
|
|
fmt.Println(line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 14:09:42 +00:00
|
|
|
// LogF is the filtered equivalent to fmt.Printf
|
2015-07-28 14:04:24 +00:00
|
|
|
func (l *Logger) LogF(level LogLevel, line string, args ...interface{}) {
|
|
|
|
if l.Level >= level {
|
|
|
|
fmt.Printf(line, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 14:09:42 +00:00
|
|
|
// ErrorF executes LogF with Error level
|
2015-07-28 14:04:24 +00:00
|
|
|
func (l *Logger) ErrorF(line string, args ...interface{}) {
|
|
|
|
l.LogF(Error, line, args...)
|
|
|
|
}
|
|
|
|
|
2015-07-28 14:09:42 +00:00
|
|
|
// InfoF executes LogF with Info level
|
2015-07-28 14:04:24 +00:00
|
|
|
func (l *Logger) InfoF(line string, args ...interface{}) {
|
|
|
|
l.LogF(Info, line, args...)
|
|
|
|
}
|
|
|
|
|
2015-07-28 14:09:42 +00:00
|
|
|
// DebugF executes LogF with Debug level
|
2015-07-28 14:04:24 +00:00
|
|
|
func (l *Logger) DebugF(line string, args ...interface{}) {
|
|
|
|
l.LogF(Debug, line, args...)
|
|
|
|
}
|