github.com/gogf/gf@v1.16.9/os/glog/glog_z_unit_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/gogf/gf.
     6  
     7  package glog_test
     8  
     9  import (
    10  	"fmt"
    11  	"github.com/gogf/gf/frame/g"
    12  	"github.com/gogf/gf/os/gfile"
    13  	"github.com/gogf/gf/os/glog"
    14  	"github.com/gogf/gf/os/gtime"
    15  	"github.com/gogf/gf/test/gtest"
    16  	"github.com/gogf/gf/text/gstr"
    17  	"testing"
    18  	"time"
    19  )
    20  
    21  func Test_Rotate_Size(t *testing.T) {
    22  	gtest.C(t, func(t *gtest.T) {
    23  		l := glog.New()
    24  		p := gfile.TempDir(gtime.TimestampNanoStr())
    25  		err := l.SetConfigWithMap(g.Map{
    26  			"Path":                 p,
    27  			"File":                 "access.log",
    28  			"StdoutPrint":          false,
    29  			"RotateSize":           10,
    30  			"RotateBackupLimit":    2,
    31  			"RotateBackupExpire":   5 * time.Second,
    32  			"RotateBackupCompress": 9,
    33  			"RotateCheckInterval":  time.Second, // For unit testing only.
    34  		})
    35  		t.Assert(err, nil)
    36  		defer gfile.Remove(p)
    37  
    38  		s := "1234567890abcdefg"
    39  		for i := 0; i < 10; i++ {
    40  			fmt.Println("logging content index:", i)
    41  			l.Print(s)
    42  		}
    43  
    44  		time.Sleep(time.Second * 3)
    45  
    46  		files, err := gfile.ScanDirFile(p, "*.gz")
    47  		t.Assert(err, nil)
    48  		t.Assert(len(files), 2)
    49  
    50  		content := gfile.GetContents(gfile.Join(p, "access.log"))
    51  		t.Assert(gstr.Count(content, s), 1)
    52  
    53  		time.Sleep(time.Second * 5)
    54  		files, err = gfile.ScanDirFile(p, "*.gz")
    55  		t.Assert(err, nil)
    56  		t.Assert(len(files), 0)
    57  	})
    58  }
    59  
    60  func Test_Rotate_Expire(t *testing.T) {
    61  	gtest.C(t, func(t *gtest.T) {
    62  		l := glog.New()
    63  		p := gfile.TempDir(gtime.TimestampNanoStr())
    64  		err := l.SetConfigWithMap(g.Map{
    65  			"Path":                 p,
    66  			"File":                 "access.log",
    67  			"StdoutPrint":          false,
    68  			"RotateExpire":         time.Second,
    69  			"RotateBackupLimit":    2,
    70  			"RotateBackupExpire":   5 * time.Second,
    71  			"RotateBackupCompress": 9,
    72  			"RotateCheckInterval":  time.Second, // For unit testing only.
    73  		})
    74  		t.Assert(err, nil)
    75  		defer gfile.Remove(p)
    76  
    77  		s := "1234567890abcdefg"
    78  		for i := 0; i < 10; i++ {
    79  			l.Print(s)
    80  		}
    81  
    82  		files, err := gfile.ScanDirFile(p, "*.gz")
    83  		t.Assert(err, nil)
    84  		t.Assert(len(files), 0)
    85  
    86  		t.Assert(gstr.Count(gfile.GetContents(gfile.Join(p, "access.log")), s), 10)
    87  
    88  		time.Sleep(time.Second * 3)
    89  
    90  		files, err = gfile.ScanDirFile(p, "*.gz")
    91  		t.Assert(err, nil)
    92  		t.Assert(len(files), 1)
    93  
    94  		t.Assert(gstr.Count(gfile.GetContents(gfile.Join(p, "access.log")), s), 0)
    95  
    96  		time.Sleep(time.Second * 5)
    97  		files, err = gfile.ScanDirFile(p, "*.gz")
    98  		t.Assert(err, nil)
    99  		t.Assert(len(files), 0)
   100  	})
   101  }