github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/logger/logger_test.go (about) 1 // (c) Copyright IBM Corp. 2021 2 // (c) Copyright Instana Inc. 2020 3 4 package logger_test 5 6 import ( 7 "os" 8 "testing" 9 10 "github.com/instana/go-sensor/logger" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestLevel_Less(t *testing.T) { 16 levels := []logger.Level{logger.ErrorLevel, logger.WarnLevel, logger.InfoLevel, logger.DebugLevel} 17 for i := range levels[:len(levels)-1] { 18 assert.True(t, levels[i+1].Less(levels[i]), "%s should be less than %s", levels[i+1], levels[i]) 19 } 20 } 21 22 func TestLogger_SetPrefix(t *testing.T) { 23 p := &printer{} 24 25 l := logger.New(p) 26 l.SetPrefix("test-logger>>") 27 l.Error("error level") 28 29 require.Len(t, p.Records, 1) 30 assert.Equal(t, []interface{}{"test-logger>>", "ERROR", ": ", "error level"}, p.Records[0]) 31 } 32 33 func TestLogger_SetPrefix_DefaultValue(t *testing.T) { 34 p := &printer{} 35 36 l := logger.New(p) 37 l.Error("error level") 38 39 require.Len(t, p.Records, 1) 40 assert.Equal(t, []interface{}{"instana: ", "ERROR", ": ", "error level"}, p.Records[0]) 41 } 42 43 func TestLogger_SetLevel(t *testing.T) { 44 examples := map[logger.Level][][]interface{}{ 45 logger.DebugLevel: { 46 {"instana: ", "DEBUG", ": ", "debuglevel"}, 47 {"instana: ", "INFO", ": ", "infolevel"}, 48 {"instana: ", "WARN", ": ", "warnlevel"}, 49 {"instana: ", "ERROR", ": ", "errorlevel"}, 50 }, 51 logger.InfoLevel: { 52 {"instana: ", "INFO", ": ", "infolevel"}, 53 {"instana: ", "WARN", ": ", "warnlevel"}, 54 {"instana: ", "ERROR", ": ", "errorlevel"}, 55 }, 56 logger.WarnLevel: { 57 {"instana: ", "WARN", ": ", "warnlevel"}, 58 {"instana: ", "ERROR", ": ", "errorlevel"}, 59 }, 60 logger.ErrorLevel: { 61 {"instana: ", "ERROR", ": ", "errorlevel"}, 62 }, 63 } 64 65 for lvl, expected := range examples { 66 t.Run(lvl.String(), func(t *testing.T) { 67 originalEnvVal, restoreOriginalVal := os.LookupEnv("INSTANA_DEBUG") 68 os.Unsetenv("INSTANA_DEBUG") 69 70 // restore original value 71 if restoreOriginalVal { 72 defer func() { 73 os.Setenv("INSTANA_DEBUG", originalEnvVal) 74 }() 75 } 76 77 p := &printer{} 78 79 l := logger.New(p) 80 l.SetLevel(lvl) 81 82 l.Debug("debug", "level") 83 l.Info("info", "level") 84 l.Warn("warn", "level") 85 l.Error("error", "level") 86 87 assert.Equal(t, expected, p.Records) 88 }) 89 } 90 91 for lvl, expected := range examples { 92 t.Run(lvl.String()+" INSTANA_LOG_LEVEL env var", func(t *testing.T) { 93 p := &printer{} 94 95 defer func() { 96 os.Unsetenv("INSTANA_LOG_LEVEL") 97 }() 98 99 os.Setenv("INSTANA_LOG_LEVEL", lvl.String()) 100 l := logger.New(p) 101 l.Debug("debug", "level") 102 l.Info("info", "level") 103 l.Warn("warn", "level") 104 l.Error("error", "level") 105 106 assert.Equal(t, expected, p.Records) 107 }) 108 } 109 110 t.Run("INSTANA_LOG_LEVEL env var replaced by SetLevel", func(t *testing.T) { 111 p := &printer{} 112 113 // restore original value 114 defer func() { 115 os.Unsetenv("INSTANA_LOG_LEVEL") 116 }() 117 118 os.Setenv("INSTANA_LOG_LEVEL", "wArn") 119 l := logger.New(p) 120 l.Debug("debug", "level") 121 l.Info("info", "level") 122 l.Warn("warn", "level") 123 l.Error("error", "level") 124 125 assert.Equal(t, examples[logger.WarnLevel], p.Records) 126 127 p.Records = p.Records[:0] 128 129 l.SetLevel(logger.InfoLevel) 130 l.Debug("debug", "level") 131 l.Info("info", "level") 132 l.Warn("warn", "level") 133 l.Error("error", "level") 134 135 assert.Equal(t, examples[logger.InfoLevel], p.Records) 136 }) 137 138 t.Run("INSTANA_DEBUG has priority over INSTANA_LOG_LEVEL env var", func(t *testing.T) { 139 p := &printer{} 140 141 os.Setenv("INSTANA_LOG_LEVEL", "wArn") 142 os.Setenv("INSTANA_DEBUG", "yes") 143 l := logger.New(p) 144 l.Debug("debug", "level") 145 l.Info("info", "level") 146 l.Warn("warn", "level") 147 l.Error("error", "level") 148 149 // The first element in p.Records is the INFO: 150 // INSTANA_DEBUG env variable is set, the log level has been set to DEBUG instead of requested WARN 151 152 assert.Equal(t, p.Records[1:], examples[logger.DebugLevel]) 153 }) 154 } 155 156 func TestLogger_SetLevel_INSTANA_DEBUG(t *testing.T) { 157 levels := []logger.Level{ 158 logger.DebugLevel, 159 logger.InfoLevel, 160 logger.WarnLevel, 161 logger.ErrorLevel, 162 } 163 for _, lvl := range levels { 164 t.Run(lvl.String(), func(t *testing.T) { 165 originalEnvVal, restoreOriginalVal := os.LookupEnv("INSTANA_DEBUG") 166 os.Setenv("INSTANA_DEBUG", "yes") 167 168 // restore original value 169 defer func() { 170 os.Unsetenv("INSTANA_DEBUG") 171 if !restoreOriginalVal { 172 os.Setenv("INSTANA_DEBUG", originalEnvVal) 173 } 174 }() 175 176 p := &printer{} 177 178 l := logger.New(p) 179 l.SetLevel(lvl) 180 181 l.Debug("debug", "level") 182 assert.Contains(t, p.Records, []interface{}{"instana: ", "DEBUG", ": ", "debuglevel"}) 183 }) 184 } 185 } 186 187 type printer struct { 188 Records [][]interface{} 189 } 190 191 func (p *printer) Print(args ...interface{}) { 192 p.Records = append(p.Records, args) 193 }