logger_test.go 803 B

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