2015-09-11 11:27:36 -04:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DebugLevel = LogLevel(0)
|
|
|
|
InfoLevel = LogLevel(1)
|
|
|
|
WarningLevel = LogLevel(2)
|
|
|
|
ErrorLevel = LogLevel(3)
|
|
|
|
)
|
|
|
|
|
|
|
|
var logLevel = WarningLevel
|
|
|
|
|
|
|
|
type LogLevel int
|
|
|
|
|
|
|
|
func SetLogLevel(level LogLevel) {
|
|
|
|
logLevel = level
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:36:21 -04:00
|
|
|
func writeLog(level LogLevel, prefix, format string, v ...interface{}) string {
|
2015-09-11 11:27:36 -04:00
|
|
|
if level < logLevel {
|
2015-09-12 14:36:21 -04:00
|
|
|
return ""
|
2015-09-11 11:27:36 -04:00
|
|
|
}
|
2015-09-12 14:36:21 -04:00
|
|
|
var data string
|
|
|
|
if v == nil || len(v) == 0 {
|
|
|
|
data = format
|
|
|
|
} else {
|
|
|
|
data = fmt.Sprintf(format, v...)
|
|
|
|
}
|
|
|
|
log.Print(prefix + data)
|
|
|
|
return data
|
2015-09-11 11:27:36 -04:00
|
|
|
}
|
|
|
|
|
2015-09-12 05:51:42 -04:00
|
|
|
func Debug(format string, v ...interface{}) {
|
2015-09-12 14:36:21 -04:00
|
|
|
writeLog(DebugLevel, "[Debug]", format, v...)
|
2015-09-12 05:51:42 -04:00
|
|
|
}
|
|
|
|
|
2015-09-11 11:27:36 -04:00
|
|
|
func Info(format string, v ...interface{}) {
|
2015-09-12 14:36:21 -04:00
|
|
|
writeLog(InfoLevel, "[Info]", format, v...)
|
2015-09-11 11:27:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func Warning(format string, v ...interface{}) {
|
2015-09-12 14:36:21 -04:00
|
|
|
writeLog(WarningLevel, "[Warning]", format, v...)
|
2015-09-11 11:27:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func Error(format string, v ...interface{}) error {
|
2015-09-12 14:36:21 -04:00
|
|
|
data := writeLog(ErrorLevel, "[Error]", format, v...)
|
2015-09-11 11:27:36 -04:00
|
|
|
return errors.New(data)
|
|
|
|
}
|