github.com/wangyougui/gf/v2@v2.6.5/os/glog/glog_z_unit_logger_rotate_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package glog_test
     8  
     9  import (
    10  	"context"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/wangyougui/gf/v2/frame/g"
    15  	"github.com/wangyougui/gf/v2/os/gfile"
    16  	"github.com/wangyougui/gf/v2/os/glog"
    17  	"github.com/wangyougui/gf/v2/os/gtime"
    18  	"github.com/wangyougui/gf/v2/test/gtest"
    19  	"github.com/wangyougui/gf/v2/text/gstr"
    20  )
    21  
    22  var (
    23  	ctx = context.TODO()
    24  )
    25  
    26  func Test_Rotate_Size(t *testing.T) {
    27  	gtest.C(t, func(t *gtest.T) {
    28  		l := glog.New()
    29  		p := gfile.Temp(gtime.TimestampNanoStr())
    30  		err := l.SetConfigWithMap(g.Map{
    31  			"Path":                 p,
    32  			"File":                 "access.log",
    33  			"StdoutPrint":          false,
    34  			"RotateSize":           10,
    35  			"RotateBackupLimit":    2,
    36  			"RotateBackupExpire":   5 * time.Second,
    37  			"RotateBackupCompress": 9,
    38  			"RotateCheckInterval":  time.Second, // For unit testing only.
    39  		})
    40  		t.AssertNil(err)
    41  		defer gfile.Remove(p)
    42  
    43  		s := "1234567890abcdefg"
    44  		for i := 0; i < 8; i++ {
    45  			l.Print(ctx, s)
    46  			time.Sleep(time.Second)
    47  		}
    48  
    49  		logFiles, err := gfile.ScanDirFile(p, "access*")
    50  		t.AssertNil(err)
    51  
    52  		for _, v := range logFiles {
    53  			content := gfile.GetContents(v)
    54  			t.AssertIN(gstr.Count(content, s), []int{1, 2})
    55  		}
    56  
    57  		time.Sleep(time.Second * 3)
    58  
    59  		files, err := gfile.ScanDirFile(p, "*.gz")
    60  		t.AssertNil(err)
    61  		t.Assert(len(files), 2)
    62  
    63  		time.Sleep(time.Second * 5)
    64  		files, err = gfile.ScanDirFile(p, "*.gz")
    65  		t.AssertNil(err)
    66  		t.Assert(len(files), 0)
    67  	})
    68  }
    69  
    70  func Test_Rotate_Expire(t *testing.T) {
    71  	gtest.C(t, func(t *gtest.T) {
    72  		l := glog.New()
    73  		p := gfile.Temp(gtime.TimestampNanoStr())
    74  		err := l.SetConfigWithMap(g.Map{
    75  			"Path":                 p,
    76  			"File":                 "access.log",
    77  			"StdoutPrint":          false,
    78  			"RotateExpire":         time.Second,
    79  			"RotateBackupLimit":    2,
    80  			"RotateBackupExpire":   5 * time.Second,
    81  			"RotateBackupCompress": 9,
    82  			"RotateCheckInterval":  time.Second, // For unit testing only.
    83  		})
    84  		t.AssertNil(err)
    85  		defer gfile.Remove(p)
    86  
    87  		s := "1234567890abcdefg"
    88  		for i := 0; i < 10; i++ {
    89  			l.Print(ctx, s)
    90  		}
    91  
    92  		files, err := gfile.ScanDirFile(p, "*.gz")
    93  		t.AssertNil(err)
    94  		t.Assert(len(files), 0)
    95  
    96  		t.Assert(gstr.Count(gfile.GetContents(gfile.Join(p, "access.log")), s), 10)
    97  
    98  		time.Sleep(time.Second * 3)
    99  
   100  		filenames, err := gfile.ScanDirFile(p, "*")
   101  		t.Log(filenames, err)
   102  		files, err = gfile.ScanDirFile(p, "*.gz")
   103  		t.AssertNil(err)
   104  		t.Assert(len(files), 1)
   105  
   106  		t.Assert(gstr.Count(gfile.GetContents(gfile.Join(p, "access.log")), s), 0)
   107  
   108  		time.Sleep(time.Second * 5)
   109  		files, err = gfile.ScanDirFile(p, "*.gz")
   110  		t.AssertNil(err)
   111  		t.Assert(len(files), 0)
   112  	})
   113  }