1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/common/log/log.go

94 lines
1.8 KiB
Go
Raw Normal View History

package log
import (
"fmt"
"io"
"os"
2015-10-13 15:41:53 -04:00
"github.com/v2ray/v2ray-core/common/platform"
)
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 {
}
func (l *noOpLogger) WriteLog(prefix, format string, v ...interface{}) {
// Swallow
}
type streamLogger struct {
writer io.Writer
}
func (l *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-10-13 15:41:53 -04:00
l.writer.Write([]byte(prefix + data + platform.LineSeparator()))
}
var (
noOpLoggerInstance logger = &noOpLogger{}
streamLoggerInstance logger = &streamLogger{
writer: os.Stdout,
}
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-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
}
func Info(format string, v ...interface{}) {
infoLogger.WriteLog("[Info]", format, v...)
}
func Warning(format string, v ...interface{}) {
warningLogger.WriteLog("[Warning]", format, v...)
}
func Error(format string, v ...interface{}) {
errorLogger.WriteLog("[Error]", format, v...)
}