2015-09-11 11:27:36 -04:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2016-05-12 02:24:41 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/log/internal"
|
2015-09-11 11:27:36 -04:00
|
|
|
)
|
|
|
|
|
2016-05-12 02:24:41 -04:00
|
|
|
type LogLevel int
|
|
|
|
|
2015-09-11 11:27:36 -04:00
|
|
|
const (
|
|
|
|
DebugLevel = LogLevel(0)
|
|
|
|
InfoLevel = LogLevel(1)
|
|
|
|
WarningLevel = LogLevel(2)
|
|
|
|
ErrorLevel = LogLevel(3)
|
2016-04-29 05:36:27 -04:00
|
|
|
NoneLevel = LogLevel(999)
|
2015-09-11 11:27:36 -04:00
|
|
|
)
|
|
|
|
|
2015-10-13 12:27:29 -04:00
|
|
|
var (
|
2016-05-12 02:24:41 -04:00
|
|
|
streamLoggerInstance internal.LogWriter = internal.NewStdOutLogWriter()
|
2015-10-13 12:27:29 -04:00
|
|
|
|
2016-05-12 02:24:41 -04:00
|
|
|
debugLogger internal.LogWriter = streamLoggerInstance
|
|
|
|
infoLogger internal.LogWriter = streamLoggerInstance
|
|
|
|
warningLogger internal.LogWriter = streamLoggerInstance
|
|
|
|
errorLogger internal.LogWriter = streamLoggerInstance
|
2015-10-13 12:27:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func SetLogLevel(level LogLevel) {
|
2016-05-12 02:24:41 -04:00
|
|
|
debugLogger = new(internal.NoOpLogWriter)
|
2015-10-13 12:27:29 -04:00
|
|
|
if level <= DebugLevel {
|
|
|
|
debugLogger = streamLoggerInstance
|
|
|
|
}
|
|
|
|
|
2016-05-12 02:24:41 -04:00
|
|
|
infoLogger = new(internal.NoOpLogWriter)
|
2015-10-13 12:27:29 -04:00
|
|
|
if level <= InfoLevel {
|
|
|
|
infoLogger = streamLoggerInstance
|
|
|
|
}
|
|
|
|
|
2016-05-12 02:24:41 -04:00
|
|
|
warningLogger = new(internal.NoOpLogWriter)
|
2015-10-13 12:27:29 -04:00
|
|
|
if level <= WarningLevel {
|
|
|
|
warningLogger = streamLoggerInstance
|
|
|
|
}
|
|
|
|
|
2016-05-12 02:24:41 -04:00
|
|
|
errorLogger = new(internal.NoOpLogWriter)
|
2015-10-13 12:27:29 -04:00
|
|
|
if level <= ErrorLevel {
|
|
|
|
errorLogger = streamLoggerInstance
|
|
|
|
}
|
2016-05-09 02:01:27 -04:00
|
|
|
|
|
|
|
if level == NoneLevel {
|
2016-05-12 02:24:41 -04:00
|
|
|
accessLoggerInstance = new(internal.NoOpLogWriter)
|
2016-05-09 02:01:27 -04:00
|
|
|
}
|
2015-09-11 11:27:36 -04:00
|
|
|
}
|
|
|
|
|
2015-12-05 15:10:14 -05:00
|
|
|
func InitErrorLogger(file string) error {
|
2016-05-12 02:24:41 -04:00
|
|
|
logger, err := internal.NewFileLogWriter(file)
|
2015-12-05 15:10:14 -05:00
|
|
|
if err != nil {
|
2016-01-18 06:24:33 -05:00
|
|
|
Error("Failed to create error logger on file (", file, "): ", err)
|
2015-12-05 15:10:14 -05:00
|
|
|
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.
|
2016-01-18 06:24:33 -05:00
|
|
|
func Debug(v ...interface{}) {
|
2016-05-12 02:24:41 -04:00
|
|
|
debugLogger.Log(&internal.ErrorLog{
|
|
|
|
Prefix: "[Debug]",
|
|
|
|
Values: v,
|
2015-12-05 15:10:14 -05:00
|
|
|
})
|
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.
|
2016-01-18 06:24:33 -05:00
|
|
|
func Info(v ...interface{}) {
|
2016-05-12 02:24:41 -04:00
|
|
|
infoLogger.Log(&internal.ErrorLog{
|
|
|
|
Prefix: "[Info]",
|
|
|
|
Values: v,
|
2015-12-05 15:10:14 -05:00
|
|
|
})
|
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.
|
2016-01-18 06:24:33 -05:00
|
|
|
func Warning(v ...interface{}) {
|
2016-05-12 02:24:41 -04:00
|
|
|
warningLogger.Log(&internal.ErrorLog{
|
|
|
|
Prefix: "[Warning]",
|
|
|
|
Values: v,
|
2015-12-05 15:10:14 -05:00
|
|
|
})
|
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.
|
2016-01-18 06:24:33 -05:00
|
|
|
func Error(v ...interface{}) {
|
2016-05-12 02:24:41 -04:00
|
|
|
errorLogger.Log(&internal.ErrorLog{
|
|
|
|
Prefix: "[Error]",
|
|
|
|
Values: v,
|
2015-12-05 15:10:14 -05:00
|
|
|
})
|
2015-09-11 11:27:36 -04:00
|
|
|
}
|