github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zlog/file_test.go (about)

     1  package zlog
     2  
     3  import (
     4  	"strconv"
     5  	"sync"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/sohaha/zlsgo"
    10  	"github.com/sohaha/zlsgo/zfile"
    11  	"github.com/sohaha/zlsgo/ztime"
    12  )
    13  
    14  func TestMain(m *testing.M) {
    15  	m.Run()
    16  	zfile.Rmdir("tmp2/")
    17  }
    18  
    19  func TestLogFile(T *testing.T) {
    20  	t := zlsgo.NewTest(T)
    21  	ResetFlags(BitLevel | BitMicroSeconds)
    22  	defer zfile.Rmdir("tmp2/")
    23  	logPath := "./tmp2/log.log"
    24  	SetSaveFile(logPath)
    25  	Success("ok1")
    26  	var ws sync.WaitGroup
    27  	for i := range make([]uint8, 100) {
    28  		ws.Add(1)
    29  		go func(i int) {
    30  			Info(i)
    31  			ws.Done()
    32  		}(i)
    33  	}
    34  
    35  	Success("ok2")
    36  	ws.Wait()
    37  	time.Sleep(time.Second * 2)
    38  
    39  	t.Equal(true, zfile.FileExist(logPath))
    40  
    41  	SetSaveFile("tmp2/ll.log", true)
    42  	Success("ok3")
    43  	Error("err3")
    44  	time.Sleep(time.Second * 2)
    45  	t.EqualTrue(zfile.DirExist("tmp2/ll"))
    46  	Discard()
    47  }
    48  
    49  func TestSetSaveFile(t *testing.T) {
    50  	log := New("TestSetSaveFile ")
    51  	log.SetFile("tmp2/test.log")
    52  	defer zfile.Rmdir("tmp2/")
    53  	log.Success("ok")
    54  	go func() {
    55  		log.SetFile("tmp2/test2.log", true)
    56  		for i := 0; i < 100; i++ {
    57  			log.Success("ok2-" + strconv.Itoa(i))
    58  		}
    59  	}()
    60  	time.Sleep(time.Second * 2)
    61  	t.Log(zfile.FileSize("tmp2/test.log"))
    62  	t.Log(zfile.FileSize("tmp2/test2/" + ztime.Now("Y-m-d") + ".log"))
    63  }