1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-06-22 18:59:55 -04:00

lazy evaluation of log fields

This commit is contained in:
v2ray
2016-01-18 12:24:33 +01:00
parent 8f20933457
commit eec0bb4db4
25 changed files with 124 additions and 117 deletions

View File

@@ -27,7 +27,7 @@ func (this *accessLog) String() string {
func InitAccessLogger(file string) error {
logger, err := newFileLogWriter(file)
if err != nil {
Error("Failed to create access logger on file (%s): %v", file, err)
Error("Failed to create access logger on file (", file, "): ", file, err)
return err
}
accessLoggerInstance = logger

View File

@@ -2,6 +2,8 @@ package log
import (
"fmt"
"github.com/v2ray/v2ray-core/common/serial"
)
const (
@@ -13,16 +15,24 @@ const (
type errorLog struct {
prefix string
format string
values []interface{}
}
func (this *errorLog) String() string {
var data string
if len(this.values) == 0 {
data = this.format
} else {
data = fmt.Sprintf(this.format, this.values...)
data := ""
for _, value := range this.values {
switch typedVal := value.(type) {
case string:
data += typedVal
case *string:
data += *typedVal
case serial.String:
data += typedVal.String()
case error:
data += typedVal.Error()
default:
data += fmt.Sprintf("%v", value)
}
}
return this.prefix + data
}
@@ -64,7 +74,7 @@ func SetLogLevel(level LogLevel) {
func InitErrorLogger(file string) error {
logger, err := newFileLogWriter(file)
if err != nil {
Error("Failed to create error logger on file (%s): %v", file, err)
Error("Failed to create error logger on file (", file, "): ", err)
return err
}
streamLoggerInstance = logger
@@ -72,37 +82,33 @@ func InitErrorLogger(file string) error {
}
// Debug outputs a debug log with given format and optional arguments.
func Debug(format string, v ...interface{}) {
func Debug(v ...interface{}) {
debugLogger.Log(&errorLog{
prefix: "[Debug]",
format: format,
values: v,
})
}
// Info outputs an info log with given format and optional arguments.
func Info(format string, v ...interface{}) {
func Info(v ...interface{}) {
infoLogger.Log(&errorLog{
prefix: "[Info]",
format: format,
values: v,
})
}
// Warning outputs a warning log with given format and optional arguments.
func Warning(format string, v ...interface{}) {
func Warning(v ...interface{}) {
warningLogger.Log(&errorLog{
prefix: "[Warning]",
format: format,
values: v,
})
}
// Error outputs an error log with given format and optional arguments.
func Error(format string, v ...interface{}) {
func Error(v ...interface{}) {
errorLogger.Log(&errorLog{
prefix: "[Error]",
format: format,
values: v,
})
}

View File

@@ -5,6 +5,7 @@ import (
"log"
"testing"
"github.com/v2ray/v2ray-core/common/serial"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
@@ -28,11 +29,11 @@ func TestStreamLogger(t *testing.T) {
infoLogger = &stdOutLogWriter{
logger: log.New(buffer, "", 0),
}
Info("Test %s Format", "Stream Logger")
assert.Bytes(buffer.Bytes()).Equals([]byte("[Info]Test Stream Logger Format\n"))
Info("Test ", "Stream Logger", " Format")
assert.StringLiteral(string(buffer.Bytes())).Equals("[Info]Test Stream Logger Format\n")
buffer.Reset()
errorLogger = infoLogger
Error("Test No Format")
assert.Bytes(buffer.Bytes()).Equals([]byte("[Error]Test No Format\n"))
Error("Test ", serial.StringLiteral("literal"), " Format")
assert.StringLiteral(string(buffer.Bytes())).Equals("[Error]Test literal Format\n")
}