trpc.group/trpc-go/trpc-go@v1.0.3/log/logger_factory_test.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 package log_test 15 16 import ( 17 "context" 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 yaml "gopkg.in/yaml.v3" 22 23 trpc "trpc.group/trpc-go/trpc-go" 24 "trpc.group/trpc-go/trpc-go/codec" 25 "trpc.group/trpc-go/trpc-go/log" 26 "trpc.group/trpc-go/trpc-go/plugin" 27 ) 28 29 func TestRegister(t *testing.T) { 30 assert.Panics(t, 31 func() { 32 log.Register("panic", nil) 33 }) 34 assert.Panics(t, func() { 35 log.Register("dup", log.NewZapLog(log.Config{})) 36 log.Register("dup", log.NewZapLog(log.Config{})) 37 }) 38 log.Register("default", log.NewZapLog(log.Config{})) 39 40 } 41 42 func TestLogFactory(t *testing.T) { 43 log.EnableTrace() 44 f := &log.Factory{} 45 46 assert.Equal(t, "log", f.Type()) 47 48 // empty decoder 49 err := f.Setup("default", nil) 50 assert.NotNil(t, err) 51 52 log.Register("default", log.DefaultLogger) 53 assert.Equal(t, log.DefaultLogger, log.Get("default")) 54 assert.Nil(t, log.Get("empty")) 55 log.Sync() 56 57 logger := log.With(log.Field{Key: "uid", Value: "1111"}) 58 assert.NotNil(t, logger) 59 logger.Debugf("test") 60 61 log.Trace("test") 62 log.Tracef("test %s", "s") 63 log.Debug("test") 64 log.Debugf("test %s", "s") 65 log.Error("test") 66 log.Errorf("test %s", "s") 67 log.Info("test") 68 log.Infof("test %s", "s") 69 log.Warn("test") 70 log.Warnf("test %s", "s") 71 72 ctx := context.Background() 73 log.TraceContext(ctx, "test") 74 log.TraceContextf(ctx, "test") 75 log.DebugContext(ctx, "test") 76 log.DebugContextf(ctx, "test") 77 log.InfoContext(ctx, "test") 78 log.InfoContextf(ctx, "test %s", "s") 79 log.ErrorContext(ctx, "test") 80 log.ErrorContextf(ctx, "test") 81 log.WarnContext(ctx, "test") 82 log.WarnContextf(ctx, "test") 83 log.WithContext(ctx, log.Field{Key: "abc", Value: 123}).Debug("testdebug") 84 85 ctx, msg := codec.WithNewMessage(ctx) 86 msg.WithCallerServiceName("trpc.test.helloworld.Greeter") 87 log.WithContextFields(ctx, "a", "a") 88 log.TraceContext(ctx, "test") 89 log.TraceContextf(ctx, "test") 90 log.DebugContext(ctx, "test") 91 log.DebugContextf(ctx, "test") 92 log.InfoContext(ctx, "test") 93 log.InfoContextf(ctx, "test %s", "s") 94 log.ErrorContext(ctx, "test") 95 log.ErrorContextf(ctx, "test") 96 log.WarnContext(ctx, "test") 97 log.WarnContextf(ctx, "test") 98 } 99 100 const configInfo = ` 101 plugins: 102 log: 103 default: 104 - writer: console # default as console std output 105 level: debug # std log level 106 - writer: file # local log file 107 level: debug # std log level 108 writer_config: # config of local file output 109 filename: trpc_time.log # the path of local rolling log files 110 roll_type: time # file rolling type 111 max_age: 7 # max expire days 112 time_unit: day # rolling time interval 113 - writer: file # local file log 114 level: debug # std output log level 115 writer_config: # config of local file output 116 filename: trpc_size.log # the path of local rolling log files 117 roll_type: size # file rolling type 118 max_age: 7 # max expire days 119 max_size: 100 # size of local rolling file, unit MB 120 max_backups: 10 # max number of log files 121 compress: false # should compress log file 122 ` 123 124 func TestLogFactorySetup(t *testing.T) { 125 cfg := trpc.Config{} 126 err := yaml.Unmarshal([]byte(configInfo), &cfg) 127 assert.Nil(t, err) 128 129 conf := cfg.Plugins["log"]["default"] 130 err = plugin.Get("log", "default").Setup("default", &plugin.YamlNodeDecoder{Node: &conf}) 131 assert.Nil(t, err) 132 133 log.Trace("test") 134 log.Debug("test") 135 log.Error("test") 136 log.Info("test") 137 log.Warn("test") 138 139 // set default. 140 log.DefaultLogger = log.NewZapLog([]log.OutputConfig{ 141 { 142 Writer: "console", 143 Level: "debug", 144 Formatter: "console", 145 }, 146 }) 147 } 148 149 const illConfigInfo = ` 150 plugins: 151 log: 152 default: 153 - writer: file # local file log 154 level: debug # std output log level 155 writer_config: # config of local file output 156 filename: # path of local file rolling log files 157 roll_type: time # rolling file type 158 max_age: 7 # max expire days 159 time_unit: day # rolling time interval 160 ` 161 162 func TestIllLogConfigPanic(t *testing.T) { 163 cfg := trpc.Config{} 164 err := yaml.Unmarshal([]byte(illConfigInfo), &cfg) 165 assert.Nil(t, err) 166 167 defer func() { 168 err := recover() 169 assert.NotNil(t, err) 170 }() 171 // NewRollWriter would return an error if file name is not configured. 172 conf := cfg.Plugins["log"]["default"] 173 plugin.Get("log", "default").Setup("default", &plugin.YamlNodeDecoder{Node: &conf}) 174 } 175 176 type fakeDecoder struct{} 177 178 func (c *fakeDecoder) Decode(conf interface{}) error { 179 return nil 180 } 181 182 func TestIllegalLogFactory(t *testing.T) { 183 cfg := trpc.Config{} 184 err := yaml.Unmarshal([]byte(configInfo), &cfg) 185 assert.Nil(t, err) 186 187 err = plugin.Get("log", "default").Setup("default", &fakeDecoder{}) 188 assert.NotNil(t, err) 189 }