github.xiaoq7.com/operator-framework/operator-sdk@v0.8.2/pkg/log/zap/logger_test.go (about) 1 // Copyright 2019 The Operator-SDK Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package zap 16 17 import ( 18 "io" 19 "os" 20 "testing" 21 "time" 22 23 "go.uber.org/zap" 24 "go.uber.org/zap/zapcore" 25 26 "github.com/go-logr/logr" 27 "github.com/go-logr/zapr" 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func TestGetConfig(t *testing.T) { 32 var opts []zap.Option 33 34 testCases := []struct { 35 name string 36 inDevel bool 37 inEncoder encoderValue 38 inLevel levelValue 39 inSample sampleValue 40 expected config 41 }{ 42 { 43 name: "development on", 44 inDevel: true, 45 inEncoder: encoderValue{ 46 set: false, 47 }, 48 inLevel: levelValue{ 49 set: false, 50 }, 51 inSample: sampleValue{ 52 set: false, 53 }, 54 expected: config{ 55 encoder: consoleEncoder(), 56 level: zap.NewAtomicLevelAt(zap.DebugLevel), 57 opts: append(opts, zap.Development(), zap.AddStacktrace(zap.ErrorLevel)), 58 sample: false, 59 }, 60 }, 61 { 62 name: "development off", 63 inDevel: false, 64 inEncoder: encoderValue{ 65 set: false, 66 }, 67 inLevel: levelValue{ 68 set: false, 69 }, 70 inSample: sampleValue{ 71 set: false, 72 }, 73 expected: config{ 74 encoder: jsonEncoder(), 75 level: zap.NewAtomicLevelAt(zap.InfoLevel), 76 opts: append(opts, zap.AddStacktrace(zap.WarnLevel)), 77 sample: true, 78 }, 79 }, 80 { 81 name: "set encoder", 82 inDevel: false, 83 inEncoder: encoderValue{ 84 set: true, 85 encoder: consoleEncoder(), 86 }, 87 inLevel: levelValue{ 88 set: false, 89 }, 90 inSample: sampleValue{ 91 set: false, 92 }, 93 expected: config{ 94 encoder: jsonEncoder(), 95 level: zap.NewAtomicLevelAt(zap.InfoLevel), 96 opts: append(opts, zap.AddStacktrace(zap.WarnLevel)), 97 sample: true, 98 }, 99 }, 100 { 101 name: "set level using level constant", 102 inDevel: false, 103 inEncoder: encoderValue{ 104 set: false, 105 }, 106 inLevel: levelValue{ 107 set: true, 108 level: zapcore.ErrorLevel, 109 }, 110 inSample: sampleValue{ 111 set: false, 112 }, 113 expected: config{ 114 encoder: jsonEncoder(), 115 level: zap.NewAtomicLevelAt(zap.ErrorLevel), 116 opts: append(opts, zap.AddStacktrace(zap.WarnLevel)), 117 sample: true, 118 }, 119 }, 120 { 121 name: "set level using custom level", 122 inDevel: false, 123 inEncoder: encoderValue{ 124 set: false, 125 }, 126 inLevel: levelValue{ 127 set: true, 128 level: zapcore.Level(-10), 129 }, 130 inSample: sampleValue{ 131 set: false, 132 }, 133 expected: config{ 134 encoder: jsonEncoder(), 135 level: zap.NewAtomicLevelAt(zapcore.Level(-10)), 136 opts: append(opts, zap.AddStacktrace(zap.WarnLevel)), 137 sample: false, 138 }, 139 }, 140 { 141 name: "set level using custom level, sample override not possible", 142 inDevel: false, 143 inEncoder: encoderValue{ 144 set: false, 145 }, 146 inLevel: levelValue{ 147 set: true, 148 level: zapcore.Level(-10), 149 }, 150 inSample: sampleValue{ 151 set: true, 152 sample: true, 153 }, 154 expected: config{ 155 encoder: jsonEncoder(), 156 level: zap.NewAtomicLevelAt(zapcore.Level(-10)), 157 opts: append(opts, zap.AddStacktrace(zap.WarnLevel)), 158 sample: false, 159 }, 160 }, 161 } 162 163 for _, tc := range testCases { 164 t.Run(tc.name, func(t *testing.T) { 165 development = tc.inDevel 166 encoderVal = tc.inEncoder 167 levelVal = tc.inLevel 168 sampleVal = tc.inSample 169 170 cfg := getConfig() 171 assert.Equal(t, tc.expected.level, cfg.level) 172 assert.Equal(t, len(tc.expected.opts), len(cfg.opts)) 173 assert.Equal(t, tc.expected.sample, cfg.sample) 174 175 dalog := createLogger(cfg, os.Stderr) 176 dalog.V(10).Info("This should not panic") 177 }) 178 } 179 } 180 181 func createLogger(cfg config, dest io.Writer) logr.Logger { 182 syncer := zapcore.AddSync(dest) 183 if cfg.sample { 184 cfg.opts = append(cfg.opts, zap.WrapCore(func(core zapcore.Core) zapcore.Core { 185 return zapcore.NewSampler(core, time.Second, 100, 100) 186 })) 187 } 188 cfg.opts = append(cfg.opts, zap.AddCallerSkip(1), zap.ErrorOutput(syncer)) 189 log := zap.New(zapcore.NewCore(cfg.encoder, syncer, cfg.level)) 190 log = log.WithOptions(cfg.opts...) 191 return zapr.NewLogger(log) 192 }