2015-09-11 11:27:36 -04:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DebugLevel = LogLevel(0)
|
|
|
|
InfoLevel = LogLevel(1)
|
|
|
|
WarningLevel = LogLevel(2)
|
|
|
|
ErrorLevel = LogLevel(3)
|
|
|
|
)
|
|
|
|
|
2015-12-05 15:10:14 -05:00
|
|
|
type errorLog struct {
|
|
|
|
prefix string
|
|
|
|
format string
|
|
|
|
values []interface{}
|
2015-10-13 12:27:29 -04:00
|
|
|
}
|
2015-09-11 11:27:36 -04:00
|
|
|
|
2015-12-05 15:10:14 -05:00
|
|
|
func (this *errorLog) String() string {
|
2015-09-12 14:36:21 -04:00
|
|
|
var data string
|
2015-12-05 15:10:14 -05:00
|
|
|
if len(this.values) == 0 {
|
|
|
|
data = this.format
|
2015-09-12 14:36:21 -04:00
|
|
|
} else {
|
2015-12-05 15:10:14 -05:00
|
|
|
data = fmt.Sprintf(this.format, this.values...)
|
2015-09-12 14:36:21 -04:00
|
|
|
}
|
2015-12-05 15:10:14 -05:00
|
|
|
return this.prefix + data
|
2015-10-13 12:27:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2015-12-05 15:10:14 -05:00
|
|
|
noOpLoggerInstance logWriter = &noOpLogWriter{}
|
|
|
|
streamLoggerInstance logWriter = newStdOutLogWriter()
|
2015-10-13 12:27:29 -04:00
|
|
|
|
|
|
|
debugLogger = noOpLoggerInstance
|
|
|
|
infoLogger = noOpLoggerInstance
|
2015-12-02 18:41:34 -05:00
|
|
|
warningLogger = streamLoggerInstance
|
|
|
|
errorLogger = streamLoggerInstance
|
2015-10-13 12:27:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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-12-05 15:10:14 -05:00
|
|
|
func InitErrorLogger(file string) error {
|
|
|
|
logger, err := newFileLogWriter(file)
|
|
|
|
if err != nil {
|
|
|
|
Error("Failed to create error logger on file (%s): %v", file, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
streamLoggerInstance = logger
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-12-05 15:10:14 -05:00
|
|
|
debugLogger.Log(&errorLog{
|
|
|
|
prefix: "[Debug]",
|
|
|
|
format: format,
|
|
|
|
values: 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-12-05 15:10:14 -05:00
|
|
|
infoLogger.Log(&errorLog{
|
|
|
|
prefix: "[Info]",
|
|
|
|
format: format,
|
|
|
|
values: 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-12-05 15:10:14 -05:00
|
|
|
warningLogger.Log(&errorLog{
|
|
|
|
prefix: "[Warning]",
|
|
|
|
format: format,
|
|
|
|
values: 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{}) {
|
2015-12-05 15:10:14 -05:00
|
|
|
errorLogger.Log(&errorLog{
|
|
|
|
prefix: "[Error]",
|
|
|
|
format: format,
|
|
|
|
values: v,
|
|
|
|
})
|
2015-09-11 11:27:36 -04:00
|
|
|
}
|