1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-20 03:16:24 -05:00
v2fly/common/log/log.go

96 lines
2.0 KiB
Go
Raw Normal View History

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