github.com/gogf/gf@v1.16.9/os/glog/glog_z_unit_chaining_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
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"github.com/gogf/gf/os/gfile"
    13  	"github.com/gogf/gf/os/gtime"
    14  	"github.com/gogf/gf/test/gtest"
    15  	"github.com/gogf/gf/text/gstr"
    16  	"testing"
    17  	"time"
    18  )
    19  
    20  func Test_To(t *testing.T) {
    21  	gtest.C(t, func(t *gtest.T) {
    22  		w := bytes.NewBuffer(nil)
    23  		To(w).Error(1, 2, 3)
    24  		To(w).Errorf("%d %d %d", 1, 2, 3)
    25  		t.Assert(gstr.Count(w.String(), defaultLevelPrefixes[LEVEL_ERRO]), 2)
    26  		t.Assert(gstr.Count(w.String(), "1 2 3"), 2)
    27  	})
    28  }
    29  
    30  func Test_Path(t *testing.T) {
    31  	gtest.C(t, func(t *gtest.T) {
    32  		path := gfile.TempDir(gtime.TimestampNanoStr())
    33  		file := fmt.Sprintf(`%d.log`, gtime.TimestampNano())
    34  
    35  		err := gfile.Mkdir(path)
    36  		t.Assert(err, nil)
    37  		defer gfile.Remove(path)
    38  
    39  		Path(path).File(file).Stdout(false).Error(1, 2, 3)
    40  		Path(path).File(file).Stdout(false).Errorf("%d %d %d", 1, 2, 3)
    41  		content := gfile.GetContents(gfile.Join(path, file))
    42  		t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 2)
    43  		t.Assert(gstr.Count(content, "1 2 3"), 2)
    44  	})
    45  }
    46  
    47  func Test_Cat(t *testing.T) {
    48  	gtest.C(t, func(t *gtest.T) {
    49  		cat := "category"
    50  		path := gfile.TempDir(gtime.TimestampNanoStr())
    51  		file := fmt.Sprintf(`%d.log`, gtime.TimestampNano())
    52  
    53  		err := gfile.Mkdir(path)
    54  		t.Assert(err, nil)
    55  		defer gfile.Remove(path)
    56  
    57  		Path(path).File(file).Cat(cat).Stdout(false).Error(1, 2, 3)
    58  		Path(path).File(file).Cat(cat).Stdout(false).Errorf("%d %d %d", 1, 2, 3)
    59  		content := gfile.GetContents(gfile.Join(path, cat, file))
    60  		t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 2)
    61  		t.Assert(gstr.Count(content, "1 2 3"), 2)
    62  	})
    63  }
    64  
    65  func Test_Level(t *testing.T) {
    66  	gtest.C(t, func(t *gtest.T) {
    67  		path := gfile.TempDir(gtime.TimestampNanoStr())
    68  		file := fmt.Sprintf(`%d.log`, gtime.TimestampNano())
    69  
    70  		err := gfile.Mkdir(path)
    71  		t.Assert(err, nil)
    72  		defer gfile.Remove(path)
    73  
    74  		Path(path).File(file).Level(LEVEL_PROD).Stdout(false).Debug(1, 2, 3)
    75  		Path(path).File(file).Level(LEVEL_PROD).Stdout(false).Debug("%d %d %d", 1, 2, 3)
    76  		content := gfile.GetContents(gfile.Join(path, file))
    77  		t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_DEBU]), 0)
    78  		t.Assert(gstr.Count(content, "1 2 3"), 0)
    79  	})
    80  }
    81  
    82  func Test_Skip(t *testing.T) {
    83  	gtest.C(t, func(t *gtest.T) {
    84  		path := gfile.TempDir(gtime.TimestampNanoStr())
    85  		file := fmt.Sprintf(`%d.log`, gtime.TimestampNano())
    86  
    87  		err := gfile.Mkdir(path)
    88  		t.Assert(err, nil)
    89  		defer gfile.Remove(path)
    90  
    91  		Path(path).File(file).Skip(10).Stdout(false).Error(1, 2, 3)
    92  		Path(path).File(file).Stdout(false).Errorf("%d %d %d", 1, 2, 3)
    93  		content := gfile.GetContents(gfile.Join(path, file))
    94  		t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 2)
    95  		t.Assert(gstr.Count(content, "1 2 3"), 2)
    96  		t.Assert(gstr.Count(content, "Stack"), 1)
    97  	})
    98  }
    99  
   100  func Test_Stack(t *testing.T) {
   101  	gtest.C(t, func(t *gtest.T) {
   102  		path := gfile.TempDir(gtime.TimestampNanoStr())
   103  		file := fmt.Sprintf(`%d.log`, gtime.TimestampNano())
   104  
   105  		err := gfile.Mkdir(path)
   106  		t.Assert(err, nil)
   107  		defer gfile.Remove(path)
   108  
   109  		Path(path).File(file).Stack(false).Stdout(false).Error(1, 2, 3)
   110  		Path(path).File(file).Stdout(false).Errorf("%d %d %d", 1, 2, 3)
   111  		content := gfile.GetContents(gfile.Join(path, file))
   112  		t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 2)
   113  		t.Assert(gstr.Count(content, "1 2 3"), 2)
   114  		t.Assert(gstr.Count(content, "Stack"), 1)
   115  	})
   116  }
   117  
   118  func Test_StackWithFilter(t *testing.T) {
   119  	gtest.C(t, func(t *gtest.T) {
   120  		path := gfile.TempDir(gtime.TimestampNanoStr())
   121  		file := fmt.Sprintf(`%d.log`, gtime.TimestampNano())
   122  
   123  		err := gfile.Mkdir(path)
   124  		t.Assert(err, nil)
   125  		defer gfile.Remove(path)
   126  
   127  		Path(path).File(file).StackWithFilter("none").Stdout(false).Error(1, 2, 3)
   128  		content := gfile.GetContents(gfile.Join(path, file))
   129  		t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 1)
   130  		t.Assert(gstr.Count(content, "1 2 3"), 1)
   131  		t.Assert(gstr.Count(content, "Stack"), 1)
   132  		fmt.Println("Content:")
   133  		fmt.Println(content)
   134  	})
   135  	gtest.C(t, func(t *gtest.T) {
   136  		path := gfile.TempDir(gtime.TimestampNanoStr())
   137  		file := fmt.Sprintf(`%d.log`, gtime.TimestampNano())
   138  
   139  		err := gfile.Mkdir(path)
   140  		t.Assert(err, nil)
   141  		defer gfile.Remove(path)
   142  
   143  		Path(path).File(file).StackWithFilter("/gf/").Stdout(false).Error(1, 2, 3)
   144  		content := gfile.GetContents(gfile.Join(path, file))
   145  		t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 1)
   146  		t.Assert(gstr.Count(content, "1 2 3"), 1)
   147  		t.Assert(gstr.Count(content, "Stack"), 0)
   148  		fmt.Println("Content:")
   149  		fmt.Println(content)
   150  	})
   151  }
   152  
   153  func Test_Header(t *testing.T) {
   154  	gtest.C(t, func(t *gtest.T) {
   155  		path := gfile.TempDir(gtime.TimestampNanoStr())
   156  		file := fmt.Sprintf(`%d.log`, gtime.TimestampNano())
   157  
   158  		err := gfile.Mkdir(path)
   159  		t.Assert(err, nil)
   160  		defer gfile.Remove(path)
   161  
   162  		Path(path).File(file).Header(true).Stdout(false).Error(1, 2, 3)
   163  		content := gfile.GetContents(gfile.Join(path, file))
   164  		t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 1)
   165  		t.Assert(gstr.Count(content, "1 2 3"), 1)
   166  	})
   167  	gtest.C(t, func(t *gtest.T) {
   168  		path := gfile.TempDir(gtime.TimestampNanoStr())
   169  		file := fmt.Sprintf(`%d.log`, gtime.TimestampNano())
   170  
   171  		err := gfile.Mkdir(path)
   172  		t.Assert(err, nil)
   173  		defer gfile.Remove(path)
   174  
   175  		Path(path).File(file).Header(false).Stdout(false).Error(1, 2, 3)
   176  		content := gfile.GetContents(gfile.Join(path, file))
   177  		t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 0)
   178  		t.Assert(gstr.Count(content, "1 2 3"), 1)
   179  	})
   180  }
   181  
   182  func Test_Line(t *testing.T) {
   183  	gtest.C(t, func(t *gtest.T) {
   184  		path := gfile.TempDir(gtime.TimestampNanoStr())
   185  		file := fmt.Sprintf(`%d.log`, gtime.TimestampNano())
   186  
   187  		err := gfile.Mkdir(path)
   188  		t.Assert(err, nil)
   189  		defer gfile.Remove(path)
   190  
   191  		Path(path).File(file).Line(true).Stdout(false).Debug(1, 2, 3)
   192  		content := gfile.GetContents(gfile.Join(path, file))
   193  		t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_DEBU]), 1)
   194  		t.Assert(gstr.Count(content, "1 2 3"), 1)
   195  		t.Assert(gstr.Count(content, ".go"), 1)
   196  		t.Assert(gstr.Contains(content, gfile.Separator), true)
   197  	})
   198  	gtest.C(t, func(t *gtest.T) {
   199  		path := gfile.TempDir(gtime.TimestampNanoStr())
   200  		file := fmt.Sprintf(`%d.log`, gtime.TimestampNano())
   201  
   202  		err := gfile.Mkdir(path)
   203  		t.Assert(err, nil)
   204  		defer gfile.Remove(path)
   205  
   206  		Path(path).File(file).Line(false).Stdout(false).Debug(1, 2, 3)
   207  		content := gfile.GetContents(gfile.Join(path, file))
   208  		t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_DEBU]), 1)
   209  		t.Assert(gstr.Count(content, "1 2 3"), 1)
   210  		t.Assert(gstr.Count(content, ".go"), 1)
   211  		t.Assert(gstr.Contains(content, gfile.Separator), false)
   212  	})
   213  }
   214  
   215  func Test_Async(t *testing.T) {
   216  	gtest.C(t, func(t *gtest.T) {
   217  		path := gfile.TempDir(gtime.TimestampNanoStr())
   218  		file := fmt.Sprintf(`%d.log`, gtime.TimestampNano())
   219  
   220  		err := gfile.Mkdir(path)
   221  		t.Assert(err, nil)
   222  		defer gfile.Remove(path)
   223  
   224  		Path(path).File(file).Async().Stdout(false).Debug(1, 2, 3)
   225  		content := gfile.GetContents(gfile.Join(path, file))
   226  		t.Assert(content, "")
   227  		time.Sleep(200 * time.Millisecond)
   228  
   229  		content = gfile.GetContents(gfile.Join(path, file))
   230  		t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_DEBU]), 1)
   231  		t.Assert(gstr.Count(content, "1 2 3"), 1)
   232  	})
   233  
   234  	gtest.C(t, func(t *gtest.T) {
   235  		path := gfile.TempDir(gtime.TimestampNanoStr())
   236  		file := fmt.Sprintf(`%d.log`, gtime.TimestampNano())
   237  
   238  		err := gfile.Mkdir(path)
   239  		t.Assert(err, nil)
   240  		defer gfile.Remove(path)
   241  
   242  		Path(path).File(file).Async(false).Stdout(false).Debug(1, 2, 3)
   243  		content := gfile.GetContents(gfile.Join(path, file))
   244  		t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_DEBU]), 1)
   245  		t.Assert(gstr.Count(content, "1 2 3"), 1)
   246  	})
   247  }