1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-23 13:44:37 -04:00
v2fly/common/log/logger_test.go

40 lines
803 B
Go
Raw Normal View History

2018-02-19 17:01:00 -05:00
package log_test
import (
"io/ioutil"
"os"
2018-07-13 08:36:09 -04:00
"strings"
2018-02-19 17:01:00 -05:00
"testing"
"time"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/buf"
. "github.com/v2fly/v2ray-core/v4/common/log"
2018-02-19 17:01:00 -05:00
)
func TestFileLogger(t *testing.T) {
f, err := ioutil.TempFile("", "vtest")
2018-07-13 08:36:09 -04:00
common.Must(err)
2018-02-19 17:01:00 -05:00
path := f.Name()
common.Must(f.Close())
creator, err := CreateFileLogWriter(path)
2018-07-13 08:36:09 -04:00
common.Must(err)
2018-02-19 17:01:00 -05: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 08:36:09 -04:00
common.Must(err)
defer f.Close()
2018-02-19 17:01:00 -05:00
b, err := buf.ReadAllToBytes(f)
2018-07-13 08:36:09 -04: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 17:01:00 -05:00
}