2015-09-11 11:27:36 -04:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-11-28 17:13:45 -05:00
|
|
|
"log"
|
2015-10-13 12:27:29 -04:00
|
|
|
"os"
|
2015-09-11 11:27:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DebugLevel = LogLevel(0)
|
|
|
|
InfoLevel = LogLevel(1)
|
|
|
|
WarningLevel = LogLevel(2)
|
|
|
|
ErrorLevel = LogLevel(3)
|
|
|
|
)
|
|
|
|
|
2015-10-13 12:27:29 -04:00
|
|
|
type logger interface {
|
|
|
|
WriteLog(prefix, format string, v ...interface{})
|
|
|
|
}
|
2015-09-11 11:27:36 -04:00
|
|
|
|
2015-10-13 12:27:29 -04:00
|
|
|
type noOpLogger struct {
|
|
|
|
}
|
2015-09-11 11:27:36 -04:00
|
|
|
|
2015-11-28 14:11:23 -05:00
|
|
|
func (this *noOpLogger) WriteLog(prefix, format string, v ...interface{}) {
|
2015-10-13 12:27:29 -04:00
|
|
|
// Swallow
|
2015-09-11 11:27:36 -04:00
|
|
|
}
|
|
|
|
|
2015-10-13 12:27:29 -04:00
|
|
|
type streamLogger struct {
|
2015-11-28 17:13:45 -05:00
|
|
|
logger *log.Logger
|
2015-10-13 12:27:29 -04:00
|
|
|
}
|
|
|
|
|
2015-11-28 14:11:23 -05:00
|
|
|
func (this *streamLogger) WriteLog(prefix, format string, v ...interface{}) {
|
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...)
|
|
|
|
}
|
2015-11-28 17:13:45 -05:00
|
|
|
this.logger.Println(prefix + data)
|
2015-10-13 12:27:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
noOpLoggerInstance logger = &noOpLogger{}
|
|
|
|
streamLoggerInstance logger = &streamLogger{
|
2015-11-28 17:13:45 -05:00
|
|
|
logger: log.New(os.Stdout, "", log.Ldate|log.Ltime),
|
2015-10-13 12:27:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
debugLogger = noOpLoggerInstance
|
|
|
|
infoLogger = noOpLoggerInstance
|
|
|
|
warningLogger = noOpLoggerInstance
|
|
|
|
errorLogger = noOpLoggerInstance
|
|
|
|
)
|
|
|
|
|
|
|
|
type LogLevel int
|
|
|
|
|
|
|
|
func SetLogLevel(level LogLevel) {
|
|
|
|
debugLogger = noOpLoggerInstance
|
|
|
|
if level <= DebugLevel {
|
|
|
|
debugLogger = streamLoggerInstance
|
|
|
|
}
|
|
|
|
|
|
|
|
infoLogger = noOpLoggerInstance
|
|
|
|
if level <= InfoLevel {
|
|
|
|
infoLogger = streamLoggerInstance
|
|
|
|
}
|
|
|
|
|
|
|
|
warningLogger = noOpLoggerInstance
|
|
|
|
if level <= WarningLevel {
|
|
|
|
warningLogger = streamLoggerInstance
|
|
|
|
}
|
|
|
|
|
|
|
|
errorLogger = noOpLoggerInstance
|
|
|
|
if level <= ErrorLevel {
|
|
|
|
errorLogger = streamLoggerInstance
|
|
|
|
}
|
2015-09-11 11:27:36 -04:00
|
|
|
}
|
|
|
|
|
2015-10-15 07:15:59 -04:00
|
|
|
// Debug outputs a debug log with given format and optional arguments.
|
2015-09-12 05:51:42 -04:00
|
|
|
func Debug(format string, v ...interface{}) {
|
2015-10-13 12:27:29 -04:00
|
|
|
debugLogger.WriteLog("[Debug]", format, v...)
|
2015-09-12 05:51:42 -04:00
|
|
|
}
|
|
|
|
|
2015-10-15 07:15:59 -04:00
|
|
|
// Info outputs an info log with given format and optional arguments.
|
2015-09-11 11:27:36 -04:00
|
|
|
func Info(format string, v ...interface{}) {
|
2015-10-13 12:27:29 -04:00
|
|
|
infoLogger.WriteLog("[Info]", format, v...)
|
2015-09-11 11:27:36 -04:00
|
|
|
}
|
|
|
|
|
2015-10-15 07:15:59 -04:00
|
|
|
// Warning outputs a warning log with given format and optional arguments.
|
2015-09-11 11:27:36 -04:00
|
|
|
func Warning(format string, v ...interface{}) {
|
2015-10-13 12:27:29 -04:00
|
|
|
warningLogger.WriteLog("[Warning]", format, v...)
|
2015-09-11 11:27:36 -04:00
|
|
|
}
|
|
|
|
|
2015-10-15 07:15:59 -04:00
|
|
|
// Error outputs an error log with given format and optional arguments.
|
2015-10-13 12:27:29 -04:00
|
|
|
func Error(format string, v ...interface{}) {
|
|
|
|
errorLogger.WriteLog("[Error]", format, v...)
|
2015-09-11 11:27:36 -04:00
|
|
|
}
|