v2ray-core/common/log/logger_test.go

40 lines
775 B
Go
Raw Normal View History

2018-02-19 22:01:00 +00:00
package log_test
import (
"io/ioutil"
"os"
2018-07-13 12:36:09 +00:00
"strings"
2018-02-19 22:01:00 +00:00
"testing"
"time"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
. "v2ray.com/core/common/log"
)
func TestFileLogger(t *testing.T) {
f, err := ioutil.TempFile("", "vtest")
2018-07-13 12:36:09 +00:00
common.Must(err)
2018-02-19 22:01:00 +00:00
path := f.Name()
common.Must(f.Close())
creator, err := CreateFileLogWriter(path)
2018-07-13 12:36:09 +00:00
common.Must(err)
2018-02-19 22:01:00 +00:00
handler := NewLogger(creator)
handler.Handle(&GeneralMessage{Content: "Test Log"})
time.Sleep(2 * time.Second)
common.Must(common.Close(handler))
f, err = os.Open(path)
2018-07-13 12:36:09 +00:00
common.Must(err)
defer f.Close() // nolint: errcheck
2018-02-19 22:01:00 +00:00
b, err := buf.ReadAllToBytes(f)
2018-07-13 12:36:09 +00:00
common.Must(err)
if !strings.Contains(string(b), "Test Log") {
t.Fatal("Expect log text contains 'Test Log', but actually: ", string(b))
}
2018-02-19 22:01:00 +00:00
}