github.com/gogf/gf/v2@v2.7.4/os/glog/glog_z_unit_logger_handler_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  	"testing"
    13  
    14  	"github.com/gogf/gf/v2/container/garray"
    15  	"github.com/gogf/gf/v2/os/glog"
    16  	"github.com/gogf/gf/v2/test/gtest"
    17  	"github.com/gogf/gf/v2/text/gstr"
    18  )
    19  
    20  var arrayForHandlerTest1 = garray.NewStrArray()
    21  
    22  func customHandler1(ctx context.Context, input *glog.HandlerInput) {
    23  	arrayForHandlerTest1.Append(input.String(false))
    24  	input.Next(ctx)
    25  }
    26  
    27  func TestLogger_SetHandlers1(t *testing.T) {
    28  	gtest.C(t, func(t *gtest.T) {
    29  		w := bytes.NewBuffer(nil)
    30  		l := glog.NewWithWriter(w)
    31  		l.SetHandlers(customHandler1)
    32  		l.SetCtxKeys("Trace-Id", "Span-Id", "Test")
    33  		ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890")
    34  		ctx = context.WithValue(ctx, "Span-Id", "abcdefg")
    35  
    36  		l.Print(ctx, 1, 2, 3)
    37  		t.Assert(gstr.Count(w.String(), "1234567890"), 1)
    38  		t.Assert(gstr.Count(w.String(), "abcdefg"), 1)
    39  		t.Assert(gstr.Count(w.String(), "1 2 3"), 1)
    40  
    41  		t.Assert(arrayForHandlerTest1.Len(), 1)
    42  		t.Assert(gstr.Count(arrayForHandlerTest1.At(0), "1234567890"), 1)
    43  		t.Assert(gstr.Count(arrayForHandlerTest1.At(0), "abcdefg"), 1)
    44  		t.Assert(gstr.Count(arrayForHandlerTest1.At(0), "1 2 3"), 1)
    45  	})
    46  }
    47  
    48  var arrayForHandlerTest2 = garray.NewStrArray()
    49  
    50  func customHandler2(ctx context.Context, input *glog.HandlerInput) {
    51  	arrayForHandlerTest2.Append(input.String(false))
    52  }
    53  
    54  func TestLogger_SetHandlers2(t *testing.T) {
    55  	gtest.C(t, func(t *gtest.T) {
    56  		w := bytes.NewBuffer(nil)
    57  		l := glog.NewWithWriter(w)
    58  		l.SetHandlers(customHandler2)
    59  		l.SetCtxKeys("Trace-Id", "Span-Id", "Test")
    60  		ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890")
    61  		ctx = context.WithValue(ctx, "Span-Id", "abcdefg")
    62  
    63  		l.Print(ctx, 1, 2, 3)
    64  		t.Assert(gstr.Count(w.String(), "1234567890"), 0)
    65  		t.Assert(gstr.Count(w.String(), "abcdefg"), 0)
    66  		t.Assert(gstr.Count(w.String(), "1 2 3"), 0)
    67  
    68  		t.Assert(arrayForHandlerTest2.Len(), 1)
    69  		t.Assert(gstr.Count(arrayForHandlerTest2.At(0), "1234567890"), 1)
    70  		t.Assert(gstr.Count(arrayForHandlerTest2.At(0), "abcdefg"), 1)
    71  		t.Assert(gstr.Count(arrayForHandlerTest2.At(0), "1 2 3"), 1)
    72  	})
    73  }
    74  
    75  func TestLogger_SetHandlers_HandlerJson(t *testing.T) {
    76  	gtest.C(t, func(t *gtest.T) {
    77  		w := bytes.NewBuffer(nil)
    78  		l := glog.NewWithWriter(w)
    79  		l.SetHandlers(glog.HandlerJson)
    80  		l.SetCtxKeys("Trace-Id", "Span-Id", "Test")
    81  		ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890")
    82  		ctx = context.WithValue(ctx, "Span-Id", "abcdefg")
    83  
    84  		l.Debug(ctx, 1, 2, 3)
    85  		t.Assert(gstr.Count(w.String(), `"CtxStr":"1234567890, abcdefg"`), 1)
    86  		t.Assert(gstr.Count(w.String(), `"Content":"1 2 3"`), 1)
    87  		t.Assert(gstr.Count(w.String(), `"Level":"DEBU"`), 1)
    88  	})
    89  }
    90  
    91  func TestLogger_SetHandlers_HandlerStructure(t *testing.T) {
    92  	gtest.C(t, func(t *gtest.T) {
    93  		w := bytes.NewBuffer(nil)
    94  		l := glog.NewWithWriter(w)
    95  		l.SetHandlers(glog.HandlerStructure)
    96  		l.SetCtxKeys("Trace-Id", "Span-Id", "Test")
    97  		ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890")
    98  		ctx = context.WithValue(ctx, "Span-Id", "abcdefg")
    99  
   100  		l.Debug(ctx, "debug", "uid", 1000)
   101  		l.Info(ctx, "info", "' '", `"\n`)
   102  
   103  		t.Assert(gstr.Count(w.String(), "uid=1000"), 1)
   104  		t.Assert(gstr.Count(w.String(), "Content=debug"), 1)
   105  		t.Assert(gstr.Count(w.String(), `"' '"="\"\\n"`), 1)
   106  		t.Assert(gstr.Count(w.String(), `CtxStr="1234567890, abcdefg"`), 2)
   107  	})
   108  }
   109  
   110  func Test_SetDefaultHandler(t *testing.T) {
   111  	gtest.C(t, func(t *gtest.T) {
   112  		oldHandler := glog.GetDefaultHandler()
   113  		glog.SetDefaultHandler(func(ctx context.Context, in *glog.HandlerInput) {
   114  			glog.HandlerJson(ctx, in)
   115  		})
   116  		defer glog.SetDefaultHandler(oldHandler)
   117  
   118  		w := bytes.NewBuffer(nil)
   119  		l := glog.NewWithWriter(w)
   120  		l.SetCtxKeys("Trace-Id", "Span-Id", "Test")
   121  		ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890")
   122  		ctx = context.WithValue(ctx, "Span-Id", "abcdefg")
   123  
   124  		l.Debug(ctx, 1, 2, 3)
   125  		t.Assert(gstr.Count(w.String(), "1234567890"), 1)
   126  		t.Assert(gstr.Count(w.String(), "abcdefg"), 1)
   127  		t.Assert(gstr.Count(w.String(), `"1 2 3"`), 1)
   128  		t.Assert(gstr.Count(w.String(), `"DEBU"`), 1)
   129  	})
   130  }