github.com/yaoapp/kun@v0.9.0/log/log_test.go (about) 1 package log 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 type output struct{ data []byte } 11 12 func (out output) String() string { 13 return string(out.data) 14 } 15 16 func (out output) Map() map[string]interface{} { 17 v := map[string]interface{}{} 18 json.Unmarshal(out.data, &v) 19 return v 20 } 21 22 func (out *output) Write(p []byte) (n int, err error) { 23 out.data = p 24 return len(out.data), nil 25 } 26 27 func (out *output) Clean() { 28 out.data = nil 29 } 30 31 var testout = &output{} 32 33 func TestSetting(t *testing.T) { 34 SetOutput(testout) 35 SetFormatter(JSON) 36 SetLevel(TraceLevel) 37 Trace("hello %s", "world") 38 data := testout.Map() 39 assert.Equal(t, "trace", data["level"]) 40 assert.Equal(t, "hello world", data["msg"]) 41 42 SetFormatter(TEXT) 43 Trace("hello %s", "world") 44 assert.Contains(t, testout.String(), `msg="hello world"`) 45 testout.Clean() 46 } 47 48 func TestWith(t *testing.T) { 49 SetOutput(testout) 50 SetFormatter(JSON) 51 With(F{"foo": "bar"}).Info("hello %s", "world") 52 data := testout.Map() 53 assert.Equal(t, "info", data["level"]) 54 assert.Equal(t, "hello world", data["msg"]) 55 assert.Equal(t, "bar", data["foo"]) 56 testout.Clean() 57 } 58 59 func TestTrace(t *testing.T) { 60 SetOutput(testout) 61 SetFormatter(JSON) 62 SetLevel(TraceLevel) 63 Trace("hello %s", "world") 64 data := testout.Map() 65 assert.Equal(t, "trace", data["level"]) 66 assert.Equal(t, "hello world", data["msg"]) 67 testout.Clean() 68 } 69 70 func TestDebug(t *testing.T) { 71 SetOutput(testout) 72 SetFormatter(JSON) 73 SetLevel(DebugLevel) 74 Trace("hello %s", "world") 75 assert.Nil(t, testout.data) 76 77 Debug("hello %s", "world") 78 data := testout.Map() 79 assert.Equal(t, "debug", data["level"]) 80 assert.Equal(t, "hello world", data["msg"]) 81 testout.Clean() 82 } 83 84 func TestInfo(t *testing.T) { 85 SetOutput(testout) 86 SetFormatter(JSON) 87 SetLevel(InfoLevel) 88 Trace("hello %s", "world") 89 assert.Nil(t, testout.data) 90 91 Debug("hello %s", "world") 92 assert.Nil(t, testout.data) 93 94 Info("hello %s", "world") 95 data := testout.Map() 96 assert.Equal(t, "info", data["level"]) 97 assert.Equal(t, "hello world", data["msg"]) 98 testout.Clean() 99 } 100 101 func TestWarn(t *testing.T) { 102 SetOutput(testout) 103 SetFormatter(JSON) 104 SetLevel(WarnLevel) 105 Trace("hello %s", "world") 106 assert.Nil(t, testout.data) 107 108 Debug("hello %s", "world") 109 assert.Nil(t, testout.data) 110 111 Info("hello %s", "world") 112 assert.Nil(t, testout.data) 113 114 Warn("hello %s", "world") 115 data := testout.Map() 116 assert.Equal(t, "warning", data["level"]) 117 assert.Equal(t, "hello world", data["msg"]) 118 testout.Clean() 119 } 120 121 func TestError(t *testing.T) { 122 SetOutput(testout) 123 SetFormatter(JSON) 124 SetLevel(ErrorLevel) 125 Trace("hello %s", "world") 126 assert.Nil(t, testout.data) 127 128 Debug("hello %s", "world") 129 assert.Nil(t, testout.data) 130 131 Info("hello %s", "world") 132 assert.Nil(t, testout.data) 133 134 Warn("hello %s", "world") 135 assert.Nil(t, testout.data) 136 137 Error("hello %s", "world") 138 data := testout.Map() 139 assert.Equal(t, "error", data["level"]) 140 assert.Equal(t, "hello world", data["msg"]) 141 testout.Clean() 142 } 143 144 func TestPanics(t *testing.T) { // Calls panic() after logging 145 SetOutput(testout) 146 SetFormatter(JSON) 147 SetLevel(PanicLevel) 148 Trace("hello %s", "world") 149 assert.Nil(t, testout.data) 150 151 Debug("hello %s", "world") 152 assert.Nil(t, testout.data) 153 154 Info("hello %s", "world") 155 assert.Nil(t, testout.data) 156 157 Warn("hello %s", "world") 158 assert.Nil(t, testout.data) 159 160 Error("hello %s", "world") 161 assert.Nil(t, testout.data) 162 163 assert.Panics(t, func() { 164 Panic("hello %s", "world") 165 }) 166 data := testout.Map() 167 assert.Equal(t, "panic", data["level"]) 168 assert.Equal(t, "hello world", data["msg"]) 169 testout.Clean() 170 }