github.com/cloudwego/kitex@v0.9.0/pkg/klog/default_test.go (about) 1 /* 2 * Copyright 2021 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package klog 18 19 import ( 20 "bytes" 21 "context" 22 "os" 23 "testing" 24 25 "github.com/cloudwego/kitex/internal/test" 26 ) 27 28 // test package level functions without format 29 func normalOutput(t *testing.T, testLevel Level, want string, args ...interface{}) { 30 buf := new(bytes.Buffer) 31 SetOutput(buf) 32 defer SetOutput(os.Stderr) 33 switch testLevel { 34 case LevelTrace: 35 Trace(args...) 36 test.Assert(t, buf.String() == want) 37 case LevelDebug: 38 Debug(args...) 39 test.Assert(t, buf.String() == want) 40 case LevelInfo: 41 Info(args...) 42 test.Assert(t, buf.String() == want) 43 case LevelNotice: 44 Notice(args...) 45 test.Assert(t, buf.String() == want) 46 case LevelWarn: 47 Warn(args...) 48 test.Assert(t, buf.String() == want) 49 case LevelError: 50 Error(args...) 51 test.Assert(t, buf.String() == want) 52 case LevelFatal: 53 t.Fatal("fatal method cannot be tested") 54 default: 55 t.Errorf("unknow level: %d", testLevel) 56 } 57 } 58 59 // test package level Ctx-related functions with 'format' 60 func ctxOutput(t *testing.T, testLevel Level, want, format string, args ...interface{}) { 61 buf := new(bytes.Buffer) 62 SetOutput(buf) 63 defer SetOutput(os.Stderr) 64 65 // the default logger implementation of CtxLogger is same as FormatLogger, no context handle now 66 ctx := context.Background() 67 68 switch testLevel { 69 case LevelTrace: 70 CtxTracef(ctx, format, args...) 71 test.Assert(t, buf.String() == want) 72 case LevelDebug: 73 CtxDebugf(ctx, format, args...) 74 test.Assert(t, buf.String() == want) 75 case LevelInfo: 76 CtxInfof(ctx, format, args...) 77 test.Assert(t, buf.String() == want) 78 case LevelNotice: 79 CtxNoticef(ctx, format, args...) 80 test.Assert(t, buf.String() == want) 81 case LevelWarn: 82 CtxWarnf(ctx, format, args...) 83 test.Assert(t, buf.String() == want) 84 case LevelError: 85 CtxErrorf(ctx, format, args...) 86 test.Assert(t, buf.String() == want) 87 case LevelFatal: 88 t.Fatal("fatal method cannot be tested") 89 default: 90 t.Errorf("unknow level: %d", testLevel) 91 } 92 } 93 94 // test package level functions with 'format' 95 func formatOutput(t *testing.T, testLevel Level, want, format string, args ...interface{}) { 96 buf := new(bytes.Buffer) 97 SetOutput(buf) 98 defer SetOutput(os.Stderr) 99 switch testLevel { 100 case LevelTrace: 101 Tracef(format, args...) 102 test.Assert(t, buf.String() == want) 103 case LevelDebug: 104 Debugf(format, args...) 105 test.Assert(t, buf.String() == want) 106 case LevelInfo: 107 Infof(format, args...) 108 test.Assert(t, buf.String() == want) 109 case LevelNotice: 110 Noticef(format, args...) 111 test.Assert(t, buf.String() == want) 112 case LevelWarn: 113 Warnf(format, args...) 114 test.Assert(t, buf.String() == want) 115 case LevelError: 116 Errorf(format, args...) 117 test.Assert(t, buf.String() == want) 118 case LevelFatal: 119 t.Fatal("fatal method cannot be tested") 120 default: 121 t.Errorf("unknow level: %d", testLevel) 122 } 123 } 124 125 func TestOutput(t *testing.T) { 126 l := DefaultLogger().(*defaultLogger) 127 oldFlags := l.stdlog.Flags() 128 l.stdlog.SetFlags(0) 129 defer l.stdlog.SetFlags(oldFlags) 130 defer SetLevel(LevelInfo) 131 132 tests := []struct { 133 format string 134 args []interface{} 135 testLevel Level 136 loggerLevel Level 137 want string 138 }{ 139 {"%s", []interface{}{"LevelNotice test"}, LevelNotice, LevelInfo, strs[LevelNotice] + "LevelNotice test\n"}, 140 {"%s %s", []interface{}{"LevelInfo", "test"}, LevelInfo, LevelWarn, ""}, 141 {"%s%s", []interface{}{"LevelDebug", "Test"}, LevelDebug, LevelDebug, strs[LevelDebug] + "LevelDebugTest\n"}, 142 {"%s", []interface{}{"LevelTrace test"}, LevelTrace, LevelTrace, strs[LevelTrace] + "LevelTrace test\n"}, 143 {"%s", []interface{}{"LevelError test"}, LevelError, LevelInfo, strs[LevelError] + "LevelError test\n"}, 144 {"%s", []interface{}{"LevelWarn test"}, LevelWarn, LevelWarn, strs[LevelWarn] + "LevelWarn test\n"}, 145 } 146 147 for _, tt := range tests { 148 SetLevel(tt.loggerLevel) 149 normalOutput(t, tt.testLevel, tt.want, tt.args...) 150 formatOutput(t, tt.testLevel, tt.want, tt.format, tt.args...) 151 ctxOutput(t, tt.testLevel, tt.want, tt.format, tt.args...) 152 } 153 }