github.com/imannamdari/v2ray-core/v5@v5.0.5/common/log/logger_test.go (about)

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