github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/common/log/logger_test.go (about)

     1  package log_test
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/xtls/xray-core/common"
    10  	"github.com/xtls/xray-core/common/buf"
    11  	. "github.com/xtls/xray-core/common/log"
    12  )
    13  
    14  func TestFileLogger(t *testing.T) {
    15  	f, err := os.CreateTemp("", "vtest")
    16  	common.Must(err)
    17  	path := f.Name()
    18  	common.Must(f.Close())
    19  
    20  	creator, err := CreateFileLogWriter(path)
    21  	common.Must(err)
    22  
    23  	handler := NewLogger(creator)
    24  	handler.Handle(&GeneralMessage{Content: "Test Log"})
    25  	time.Sleep(2 * time.Second)
    26  
    27  	common.Must(common.Close(handler))
    28  
    29  	f, err = os.Open(path)
    30  	common.Must(err)
    31  	defer f.Close()
    32  
    33  	b, err := buf.ReadAllToBytes(f)
    34  	common.Must(err)
    35  	if !strings.Contains(string(b), "Test Log") {
    36  		t.Fatal("Expect log text contains 'Test Log', but actually: ", string(b))
    37  	}
    38  }