github.com/gogf/gf/v2@v2.7.4/os/glog/glog_z_unit_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  	"bytes"
    11  	"context"
    12  	"os"
    13  	"strings"
    14  	"sync"
    15  	"testing"
    16  	"time"
    17  
    18  	"github.com/gogf/gf/v2/frame/g"
    19  	"github.com/gogf/gf/v2/os/gfile"
    20  	"github.com/gogf/gf/v2/os/glog"
    21  	"github.com/gogf/gf/v2/os/gtime"
    22  	"github.com/gogf/gf/v2/test/gtest"
    23  	"github.com/gogf/gf/v2/text/gstr"
    24  )
    25  
    26  func TestCase(t *testing.T) {
    27  	defaultLog := glog.DefaultLogger().Clone()
    28  	defer glog.SetDefaultLogger(defaultLog)
    29  
    30  	gtest.C(t, func(t *gtest.T) {
    31  		t.AssertNE(glog.Instance(), nil)
    32  	})
    33  }
    34  
    35  func TestDefaultLogger(t *testing.T) {
    36  	defaultLog := glog.DefaultLogger().Clone()
    37  	defer glog.SetDefaultLogger(defaultLog)
    38  
    39  	gtest.C(t, func(t *gtest.T) {
    40  		t.AssertNE(defaultLog, nil)
    41  		log := glog.New()
    42  		glog.SetDefaultLogger(log)
    43  		t.AssertEQ(glog.DefaultLogger(), defaultLog)
    44  		t.AssertEQ(glog.Expose(), defaultLog)
    45  	})
    46  }
    47  
    48  func TestAPI(t *testing.T) {
    49  	gtest.C(t, func(t *gtest.T) {
    50  		glog.Print(ctx, "Print")
    51  		glog.Printf(ctx, "%s", "Printf")
    52  		glog.Info(ctx, "Info")
    53  		glog.Infof(ctx, "%s", "Infof")
    54  		glog.Debug(ctx, "Debug")
    55  		glog.Debugf(ctx, "%s", "Debugf")
    56  		glog.Notice(ctx, "Notice")
    57  		glog.Noticef(ctx, "%s", "Noticef")
    58  		glog.Warning(ctx, "Warning")
    59  		glog.Warningf(ctx, "%s", "Warningf")
    60  		glog.Error(ctx, "Error")
    61  		glog.Errorf(ctx, "%s", "Errorf")
    62  		glog.Critical(ctx, "Critical")
    63  		glog.Criticalf(ctx, "%s", "Criticalf")
    64  	})
    65  }
    66  
    67  func TestChaining(t *testing.T) {
    68  	defaultLog := glog.DefaultLogger().Clone()
    69  	defer glog.SetDefaultLogger(defaultLog)
    70  
    71  	gtest.C(t, func(t *gtest.T) {
    72  		t.AssertNE(glog.Cat("module"), nil)
    73  		t.AssertNE(glog.File("test.log"), nil)
    74  		t.AssertNE(glog.Level(glog.LEVEL_ALL), nil)
    75  		t.AssertNE(glog.LevelStr("all"), nil)
    76  		t.AssertNE(glog.Skip(1), nil)
    77  		t.AssertNE(glog.Stack(false), nil)
    78  		t.AssertNE(glog.StackWithFilter("none"), nil)
    79  		t.AssertNE(glog.Stdout(false), nil)
    80  		t.AssertNE(glog.Header(false), nil)
    81  		t.AssertNE(glog.Line(false), nil)
    82  		t.AssertNE(glog.Async(false), nil)
    83  	})
    84  }
    85  
    86  func Test_SetFile(t *testing.T) {
    87  	defaultLog := glog.DefaultLogger().Clone()
    88  	defer glog.SetDefaultLogger(defaultLog)
    89  	gtest.C(t, func(t *gtest.T) {
    90  		glog.SetFile("test.log")
    91  	})
    92  }
    93  
    94  func Test_SetTimeFormat(t *testing.T) {
    95  	gtest.C(t, func(t *gtest.T) {
    96  		w := bytes.NewBuffer(nil)
    97  		l := glog.NewWithWriter(w)
    98  
    99  		l.SetTimeFormat("2006-01-02T15:04:05.000Z07:00")
   100  		l.Debug(ctx, "test")
   101  
   102  		t.AssertGE(len(strings.Split(w.String(), "[DEBU]")), 1)
   103  		datetime := strings.Trim(strings.Split(w.String(), "[DEBU]")[0], " ")
   104  
   105  		_, err := time.Parse("2006-01-02T15:04:05.000Z07:00", datetime)
   106  		t.AssertNil(err)
   107  		_, err = time.Parse("2006-01-02 15:04:05.000", datetime)
   108  		t.AssertNE(err, nil)
   109  		_, err = time.Parse("Mon, 02 Jan 2006 15:04:05 -0700", datetime)
   110  		t.AssertNE(err, nil)
   111  	})
   112  }
   113  
   114  func Test_SetLevel(t *testing.T) {
   115  	defaultLog := glog.DefaultLogger().Clone()
   116  	defer glog.SetDefaultLogger(defaultLog)
   117  	gtest.C(t, func(t *gtest.T) {
   118  		glog.SetLevel(glog.LEVEL_ALL)
   119  		t.Assert(glog.GetLevel()&glog.LEVEL_ALL, glog.LEVEL_ALL)
   120  	})
   121  }
   122  
   123  func Test_SetAsync(t *testing.T) {
   124  	defaultLog := glog.DefaultLogger().Clone()
   125  	defer glog.SetDefaultLogger(defaultLog)
   126  	gtest.C(t, func(t *gtest.T) {
   127  		glog.SetAsync(false)
   128  	})
   129  }
   130  
   131  func Test_SetStdoutPrint(t *testing.T) {
   132  	defaultLog := glog.DefaultLogger().Clone()
   133  	defer glog.SetDefaultLogger(defaultLog)
   134  	gtest.C(t, func(t *gtest.T) {
   135  		glog.SetStdoutPrint(false)
   136  	})
   137  }
   138  
   139  func Test_SetHeaderPrint(t *testing.T) {
   140  	defaultLog := glog.DefaultLogger().Clone()
   141  	defer glog.SetDefaultLogger(defaultLog)
   142  	gtest.C(t, func(t *gtest.T) {
   143  		glog.SetHeaderPrint(false)
   144  	})
   145  }
   146  
   147  func Test_SetPrefix(t *testing.T) {
   148  	defaultLog := glog.DefaultLogger().Clone()
   149  	defer glog.SetDefaultLogger(defaultLog)
   150  	gtest.C(t, func(t *gtest.T) {
   151  		glog.SetPrefix("log_prefix")
   152  	})
   153  }
   154  
   155  func Test_SetConfigWithMap(t *testing.T) {
   156  	defaultLog := glog.DefaultLogger().Clone()
   157  	defer glog.SetDefaultLogger(defaultLog)
   158  	gtest.C(t, func(t *gtest.T) {
   159  		t.Assert(glog.SetConfigWithMap(map[string]interface{}{
   160  			"level": "all",
   161  		}), nil)
   162  	})
   163  }
   164  
   165  func Test_SetPath(t *testing.T) {
   166  	defaultLog := glog.DefaultLogger().Clone()
   167  	defer glog.SetDefaultLogger(defaultLog)
   168  	gtest.C(t, func(t *gtest.T) {
   169  		t.Assert(glog.SetPath("/var/log"), nil)
   170  		t.Assert(glog.GetPath(), "/var/log")
   171  	})
   172  }
   173  
   174  func Test_SetWriter(t *testing.T) {
   175  	defaultLog := glog.DefaultLogger().Clone()
   176  	defer glog.SetDefaultLogger(defaultLog)
   177  	gtest.C(t, func(t *gtest.T) {
   178  		glog.SetWriter(os.Stdout)
   179  		t.Assert(glog.GetWriter(), os.Stdout)
   180  	})
   181  }
   182  
   183  func Test_SetFlags(t *testing.T) {
   184  	defaultLog := glog.DefaultLogger().Clone()
   185  	defer glog.SetDefaultLogger(defaultLog)
   186  	gtest.C(t, func(t *gtest.T) {
   187  		glog.SetFlags(glog.F_ASYNC)
   188  		t.Assert(glog.GetFlags(), glog.F_ASYNC)
   189  	})
   190  }
   191  
   192  func Test_SetCtxKeys(t *testing.T) {
   193  	defaultLog := glog.DefaultLogger().Clone()
   194  	defer glog.SetDefaultLogger(defaultLog)
   195  	gtest.C(t, func(t *gtest.T) {
   196  		glog.SetCtxKeys("SpanId", "TraceId")
   197  		t.Assert(glog.GetCtxKeys(), []string{"SpanId", "TraceId"})
   198  	})
   199  }
   200  
   201  func Test_PrintStack(t *testing.T) {
   202  	defaultLog := glog.DefaultLogger().Clone()
   203  	defer glog.SetDefaultLogger(defaultLog)
   204  	gtest.C(t, func(t *gtest.T) {
   205  		glog.PrintStack(ctx, 1)
   206  	})
   207  }
   208  
   209  func Test_SetStack(t *testing.T) {
   210  	defaultLog := glog.DefaultLogger().Clone()
   211  	defer glog.SetDefaultLogger(defaultLog)
   212  	gtest.C(t, func(t *gtest.T) {
   213  		glog.SetStack(true)
   214  		t.Assert(glog.GetStack(1), "")
   215  	})
   216  }
   217  
   218  func Test_SetLevelStr(t *testing.T) {
   219  	defaultLog := glog.DefaultLogger().Clone()
   220  	defer glog.SetDefaultLogger(defaultLog)
   221  	gtest.C(t, func(t *gtest.T) {
   222  		t.Assert(glog.SetLevelStr("all"), nil)
   223  	})
   224  	gtest.C(t, func(t *gtest.T) {
   225  		l := glog.New()
   226  		t.AssertNE(l.SetLevelStr("test"), nil)
   227  	})
   228  }
   229  
   230  func Test_SetLevelPrefix(t *testing.T) {
   231  	defaultLog := glog.DefaultLogger().Clone()
   232  	defer glog.SetDefaultLogger(defaultLog)
   233  	gtest.C(t, func(t *gtest.T) {
   234  		glog.SetLevelPrefix(glog.LEVEL_ALL, "LevelPrefix")
   235  		t.Assert(glog.GetLevelPrefix(glog.LEVEL_ALL), "LevelPrefix")
   236  	})
   237  }
   238  
   239  func Test_SetLevelPrefixes(t *testing.T) {
   240  	defaultLog := glog.DefaultLogger().Clone()
   241  	defer glog.SetDefaultLogger(defaultLog)
   242  	gtest.C(t, func(t *gtest.T) {
   243  		glog.SetLevelPrefixes(map[int]string{
   244  			glog.LEVEL_ALL: "ALL_Prefix",
   245  		})
   246  	})
   247  }
   248  
   249  func Test_SetHandlers(t *testing.T) {
   250  	defaultLog := glog.DefaultLogger().Clone()
   251  	defer glog.SetDefaultLogger(defaultLog)
   252  	gtest.C(t, func(t *gtest.T) {
   253  		glog.SetHandlers(func(ctx context.Context, in *glog.HandlerInput) {
   254  		})
   255  	})
   256  }
   257  
   258  func Test_SetWriterColorEnable(t *testing.T) {
   259  	defaultLog := glog.DefaultLogger().Clone()
   260  	defer glog.SetDefaultLogger(defaultLog)
   261  	gtest.C(t, func(t *gtest.T) {
   262  		glog.SetWriterColorEnable(true)
   263  	})
   264  }
   265  
   266  func Test_Instance(t *testing.T) {
   267  	defaultLog := glog.DefaultLogger().Clone()
   268  	defer glog.SetDefaultLogger(defaultLog)
   269  	gtest.C(t, func(t *gtest.T) {
   270  		t.AssertNE(glog.Instance("gf"), nil)
   271  	})
   272  }
   273  
   274  func Test_GetConfig(t *testing.T) {
   275  	gtest.C(t, func(t *gtest.T) {
   276  		config := glog.DefaultLogger().GetConfig()
   277  		t.Assert(config.Path, "")
   278  		t.Assert(config.StdoutPrint, true)
   279  	})
   280  }
   281  
   282  func Test_Write(t *testing.T) {
   283  	gtest.C(t, func(t *gtest.T) {
   284  		l := glog.New()
   285  		len, err := l.Write([]byte("GoFrame"))
   286  		t.AssertNil(err)
   287  		t.Assert(len, 7)
   288  	})
   289  }
   290  
   291  func Test_Chaining_To(t *testing.T) {
   292  	gtest.C(t, func(t *gtest.T) {
   293  		l := glog.DefaultLogger().Clone()
   294  		logTo := l.To(os.Stdout)
   295  		t.AssertNE(logTo, nil)
   296  	})
   297  	gtest.C(t, func(t *gtest.T) {
   298  		l := glog.New()
   299  		logTo := l.To(os.Stdout)
   300  		t.AssertNE(logTo, nil)
   301  	})
   302  }
   303  
   304  func Test_Chaining_Path(t *testing.T) {
   305  	gtest.C(t, func(t *gtest.T) {
   306  		l := glog.DefaultLogger().Clone()
   307  		logPath := l.Path("./")
   308  		t.AssertNE(logPath, nil)
   309  	})
   310  	gtest.C(t, func(t *gtest.T) {
   311  		l := glog.New()
   312  		logPath := l.Path("./")
   313  		t.AssertNE(logPath, nil)
   314  	})
   315  }
   316  
   317  func Test_Chaining_Cat(t *testing.T) {
   318  	gtest.C(t, func(t *gtest.T) {
   319  		l := glog.New()
   320  		logCat := l.Cat(".gf")
   321  		t.AssertNE(logCat, nil)
   322  	})
   323  }
   324  
   325  func Test_Chaining_Level(t *testing.T) {
   326  	gtest.C(t, func(t *gtest.T) {
   327  		l := glog.New()
   328  		logLevel := l.Level(glog.LEVEL_ALL)
   329  		t.AssertNE(logLevel, nil)
   330  	})
   331  }
   332  
   333  func Test_Chaining_LevelStr(t *testing.T) {
   334  	gtest.C(t, func(t *gtest.T) {
   335  		l := glog.New()
   336  		logLevelStr := l.LevelStr("all")
   337  		t.AssertNE(logLevelStr, nil)
   338  	})
   339  }
   340  
   341  func Test_Chaining_Skip(t *testing.T) {
   342  	gtest.C(t, func(t *gtest.T) {
   343  		l := glog.New()
   344  		logSkip := l.Skip(1)
   345  		t.AssertNE(logSkip, nil)
   346  	})
   347  }
   348  
   349  func Test_Chaining_Stack(t *testing.T) {
   350  	gtest.C(t, func(t *gtest.T) {
   351  		l := glog.New()
   352  		logStack := l.Stack(true)
   353  		t.AssertNE(logStack, nil)
   354  	})
   355  }
   356  
   357  func Test_Chaining_StackWithFilter(t *testing.T) {
   358  	gtest.C(t, func(t *gtest.T) {
   359  		l := glog.New()
   360  		logStackWithFilter := l.StackWithFilter("gtest")
   361  		t.AssertNE(logStackWithFilter, nil)
   362  	})
   363  }
   364  
   365  func Test_Chaining_Stdout(t *testing.T) {
   366  	gtest.C(t, func(t *gtest.T) {
   367  		l := glog.New()
   368  		logStdout := l.Stdout(true)
   369  		t.AssertNE(logStdout, nil)
   370  	})
   371  }
   372  
   373  func Test_Chaining_Header(t *testing.T) {
   374  	gtest.C(t, func(t *gtest.T) {
   375  		l := glog.New()
   376  		logHeader := l.Header(true)
   377  		t.AssertNE(logHeader, nil)
   378  	})
   379  }
   380  
   381  func Test_Chaining_Line(t *testing.T) {
   382  	gtest.C(t, func(t *gtest.T) {
   383  		l := glog.New()
   384  		logLine := l.Line(true)
   385  		t.AssertNE(logLine, nil)
   386  	})
   387  }
   388  
   389  func Test_Chaining_Async(t *testing.T) {
   390  	gtest.C(t, func(t *gtest.T) {
   391  		l := glog.New()
   392  		logAsync := l.Async(true)
   393  		t.AssertNE(logAsync, nil)
   394  	})
   395  }
   396  
   397  func Test_Config_SetDebug(t *testing.T) {
   398  	gtest.C(t, func(t *gtest.T) {
   399  		l := glog.New()
   400  		l.SetDebug(false)
   401  	})
   402  }
   403  
   404  func Test_Config_AppendCtxKeys(t *testing.T) {
   405  	gtest.C(t, func(t *gtest.T) {
   406  		l := glog.New()
   407  		l.AppendCtxKeys("Trace-Id", "Span-Id", "Test")
   408  		l.AppendCtxKeys("Trace-Id-New", "Span-Id-New", "Test")
   409  	})
   410  }
   411  
   412  func Test_Config_SetPath(t *testing.T) {
   413  	gtest.C(t, func(t *gtest.T) {
   414  		l := glog.New()
   415  		t.AssertNE(l.SetPath(""), nil)
   416  	})
   417  }
   418  
   419  func Test_Config_SetStdoutColorDisabled(t *testing.T) {
   420  	gtest.C(t, func(t *gtest.T) {
   421  		l := glog.New()
   422  		l.SetStdoutColorDisabled(false)
   423  	})
   424  }
   425  
   426  func Test_Ctx(t *testing.T) {
   427  	gtest.C(t, func(t *gtest.T) {
   428  		w := bytes.NewBuffer(nil)
   429  		l := glog.NewWithWriter(w)
   430  		l.SetCtxKeys("Trace-Id", "Span-Id", "Test")
   431  		ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890")
   432  		ctx = context.WithValue(ctx, "Span-Id", "abcdefg")
   433  
   434  		l.Print(ctx, 1, 2, 3)
   435  		t.Assert(gstr.Count(w.String(), "1234567890"), 1)
   436  		t.Assert(gstr.Count(w.String(), "abcdefg"), 1)
   437  		t.Assert(gstr.Count(w.String(), "1 2 3"), 1)
   438  	})
   439  }
   440  
   441  func Test_Ctx_Config(t *testing.T) {
   442  	gtest.C(t, func(t *gtest.T) {
   443  		w := bytes.NewBuffer(nil)
   444  		l := glog.NewWithWriter(w)
   445  		m := map[string]interface{}{
   446  			"CtxKeys": g.SliceStr{"Trace-Id", "Span-Id", "Test"},
   447  		}
   448  		var nilMap map[string]interface{}
   449  
   450  		err := l.SetConfigWithMap(m)
   451  		t.AssertNil(err)
   452  		err = l.SetConfigWithMap(nilMap)
   453  		t.AssertNE(err, nil)
   454  
   455  		ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890")
   456  		ctx = context.WithValue(ctx, "Span-Id", "abcdefg")
   457  
   458  		l.Print(ctx, 1, 2, 3)
   459  		t.Assert(gstr.Count(w.String(), "1234567890"), 1)
   460  		t.Assert(gstr.Count(w.String(), "abcdefg"), 1)
   461  		t.Assert(gstr.Count(w.String(), "1 2 3"), 1)
   462  	})
   463  }
   464  
   465  func Test_Concurrent(t *testing.T) {
   466  	gtest.C(t, func(t *gtest.T) {
   467  		c := 1000
   468  		l := glog.New()
   469  		s := "@1234567890#"
   470  		f := "test.log"
   471  		p := gfile.Temp(gtime.TimestampNanoStr())
   472  		t.Assert(l.SetPath(p), nil)
   473  		defer gfile.Remove(p)
   474  		wg := sync.WaitGroup{}
   475  		ch := make(chan struct{})
   476  		for i := 0; i < c; i++ {
   477  			wg.Add(1)
   478  			go func() {
   479  				defer wg.Done()
   480  				<-ch
   481  				l.File(f).Stdout(false).Print(ctx, s)
   482  			}()
   483  		}
   484  		close(ch)
   485  		wg.Wait()
   486  		content := gfile.GetContents(gfile.Join(p, f))
   487  		t.Assert(gstr.Count(content, s), c)
   488  	})
   489  }