logger_test.go 775 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package log_test
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "strings"
  6. "testing"
  7. "time"
  8. "v2ray.com/core/common"
  9. "v2ray.com/core/common/buf"
  10. . "v2ray.com/core/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() // nolint: errcheck
  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. }