// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. package glog_test import ( "bytes" "context" "sync" "testing" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gfile" "github.com/gogf/gf/v2/os/glog" "github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/test/gtest" "github.com/gogf/gf/v2/text/gstr" ) func Test_Ctx(t *testing.T) { gtest.C(t, func(t *gtest.T) { w := bytes.NewBuffer(nil) l := glog.NewWithWriter(w) l.SetCtxKeys("Trace-Id", "Span-Id", "Test") ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890") ctx = context.WithValue(ctx, "Span-Id", "abcdefg") l.Print(ctx, 1, 2, 3) t.Assert(gstr.Count(w.String(), "1234567890"), 1) t.Assert(gstr.Count(w.String(), "abcdefg"), 1) t.Assert(gstr.Count(w.String(), "1 2 3"), 1) }) } func Test_Ctx_Config(t *testing.T) { gtest.C(t, func(t *gtest.T) { w := bytes.NewBuffer(nil) l := glog.NewWithWriter(w) m := map[string]interface{}{ "CtxKeys": g.SliceStr{"Trace-Id", "Span-Id", "Test"}, } err := l.SetConfigWithMap(m) t.Assert(err, nil) ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890") ctx = context.WithValue(ctx, "Span-Id", "abcdefg") l.Print(ctx, 1, 2, 3) t.Assert(gstr.Count(w.String(), "1234567890"), 1) t.Assert(gstr.Count(w.String(), "abcdefg"), 1) t.Assert(gstr.Count(w.String(), "1 2 3"), 1) }) } func Test_Ctx_CtxKey(t *testing.T) { gtest.C(t, func(t *gtest.T) { w := bytes.NewBuffer(nil) l := glog.NewWithWriter(w) l.Print(gctx.WithCtxId(context.TODO(), "abcdefg"), 1, 2, 3) t.Assert(gstr.Count(w.String(), "abcdefg"), 1) t.Assert(gstr.Count(w.String(), "1 2 3"), 1) }) } func Test_Concurrent(t *testing.T) { gtest.C(t, func(t *gtest.T) { c := 1000 l := glog.New() s := "@1234567890#" f := "test.log" p := gfile.TempDir(gtime.TimestampNanoStr()) t.Assert(l.SetPath(p), nil) defer gfile.Remove(p) wg := sync.WaitGroup{} ch := make(chan struct{}) for i := 0; i < c; i++ { wg.Add(1) go func() { defer wg.Done() <-ch l.File(f).Stdout(false).Print(ctx, s) }() } close(ch) wg.Wait() content := gfile.GetContents(gfile.Join(p, f)) t.Assert(gstr.Count(content, s), c) }) }