mirror of https://github.com/v2ray/v2ray-core
parent
a37819c330
commit
abdcda0a2f
@ -1,36 +0,0 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/serial"
|
||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||
"github.com/v2ray/v2ray-core/testing/assert"
|
||||
)
|
||||
|
||||
func TestAccessLog(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
filename := "/tmp/test_access_log.log"
|
||||
InitAccessLogger(filename)
|
||||
_, err := os.Stat(filename)
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
Access(serial.StringLiteral("test_from"), serial.StringLiteral("test_to"), AccessAccepted, serial.StringLiteral("test_reason"))
|
||||
<-time.After(2 * time.Second)
|
||||
|
||||
accessLoggerInstance.(*fileLogWriter).close()
|
||||
accessLoggerInstance = &noOpLogWriter{}
|
||||
|
||||
content, err := ioutil.ReadFile(filename)
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
contentStr := serial.StringLiteral(content)
|
||||
assert.String(contentStr).Contains(serial.StringLiteral("test_from"))
|
||||
assert.String(contentStr).Contains(serial.StringLiteral("test_to"))
|
||||
assert.String(contentStr).Contains(serial.StringLiteral("test_reason"))
|
||||
assert.String(contentStr).Contains(serial.StringLiteral("accepted"))
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common"
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
"github.com/v2ray/v2ray-core/common/serial"
|
||||
)
|
||||
|
||||
type LogEntry interface {
|
||||
common.Releasable
|
||||
serial.String
|
||||
}
|
||||
|
||||
type ErrorLog struct {
|
||||
Prefix string
|
||||
Values []interface{}
|
||||
}
|
||||
|
||||
func (this *ErrorLog) Release() {
|
||||
for index := range this.Values {
|
||||
this.Values[index] = nil
|
||||
}
|
||||
this.Values = nil
|
||||
}
|
||||
|
||||
func (this *ErrorLog) String() string {
|
||||
b := alloc.NewSmallBuffer().Clear()
|
||||
defer b.Release()
|
||||
|
||||
b.AppendString(this.Prefix)
|
||||
|
||||
for _, value := range this.Values {
|
||||
switch typedVal := value.(type) {
|
||||
case string:
|
||||
b.AppendString(typedVal)
|
||||
case *string:
|
||||
b.AppendString(*typedVal)
|
||||
case serial.String:
|
||||
b.AppendString(typedVal.String())
|
||||
case error:
|
||||
b.AppendString(typedVal.Error())
|
||||
default:
|
||||
b.AppendString(fmt.Sprint(value))
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
type AccessLog struct {
|
||||
From serial.String
|
||||
To serial.String
|
||||
Status string
|
||||
Reason serial.String
|
||||
}
|
||||
|
||||
func (this *AccessLog) Release() {
|
||||
this.From = nil
|
||||
this.To = nil
|
||||
this.Reason = nil
|
||||
}
|
||||
|
||||
func (this *AccessLog) String() string {
|
||||
b := alloc.NewSmallBuffer().Clear()
|
||||
defer b.Release()
|
||||
|
||||
return b.AppendString(this.From.String()).AppendString(" ").AppendString(this.Status).AppendString(" ").AppendString(this.To.String()).AppendString(" ").AppendString(this.Reason.String()).String()
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package internal_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/v2ray/v2ray-core/common/log/internal"
|
||||
"github.com/v2ray/v2ray-core/common/serial"
|
||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||
"github.com/v2ray/v2ray-core/testing/assert"
|
||||
)
|
||||
|
||||
func TestAccessLog(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
entry := &AccessLog{
|
||||
From: serial.StringLiteral("test_from"),
|
||||
To: serial.StringLiteral("test_to"),
|
||||
Status: "Accepted",
|
||||
Reason: serial.StringLiteral("test_reason"),
|
||||
}
|
||||
|
||||
entryStr := entry.String()
|
||||
assert.StringLiteral(entryStr).Contains(serial.StringLiteral("test_from"))
|
||||
assert.StringLiteral(entryStr).Contains(serial.StringLiteral("test_to"))
|
||||
assert.StringLiteral(entryStr).Contains(serial.StringLiteral("test_reason"))
|
||||
assert.StringLiteral(entryStr).Contains(serial.StringLiteral("Accepted"))
|
||||
}
|
@ -0,0 +1 @@
|
||||
package internal_test
|
@ -1,40 +0,0 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/platform"
|
||||
"github.com/v2ray/v2ray-core/common/serial"
|
||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||
"github.com/v2ray/v2ray-core/testing/assert"
|
||||
)
|
||||
|
||||
func TestLogLevelSetting(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
assert.Pointer(debugLogger).Equals(noOpLoggerInstance)
|
||||
SetLogLevel(DebugLevel)
|
||||
assert.Pointer(debugLogger).Equals(streamLoggerInstance)
|
||||
|
||||
SetLogLevel(InfoLevel)
|
||||
assert.Pointer(debugLogger).Equals(noOpLoggerInstance)
|
||||
assert.Pointer(infoLogger).Equals(streamLoggerInstance)
|
||||
}
|
||||
|
||||
func TestStreamLogger(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
buffer := bytes.NewBuffer(make([]byte, 0, 1024))
|
||||
infoLogger = &stdOutLogWriter{
|
||||
logger: log.New(buffer, "", 0),
|
||||
}
|
||||
Info("Test ", "Stream Logger", " Format")
|
||||
assert.StringLiteral(string(buffer.Bytes())).Equals("[Info]Test Stream Logger Format" + platform.LineSeparator())
|
||||
|
||||
buffer.Reset()
|
||||
errorLogger = infoLogger
|
||||
Error("Test ", serial.StringLiteral("literal"), " Format")
|
||||
assert.StringLiteral(string(buffer.Bytes())).Equals("[Error]Test literal Format" + platform.LineSeparator())
|
||||
}
|
Loading…
Reference in new issue