github.com/swiftstack/proxyfs@v0.0.0-20201223034610-5434d919416e/logger/api_test.go (about) 1 package logger 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 10 "github.com/swiftstack/ProxyFS/conf" 11 "github.com/swiftstack/ProxyFS/utils" 12 ) 13 14 func testNestedFunc() { 15 myint := 3 16 myctx := TraceEnter("my_args_prefix", 1, myint) 17 defer myctx.TraceExit("my_no_args_prefix") 18 19 test2ndNestedFunc() 20 } 21 22 func test2ndNestedFunc() { 23 myint := 77 24 myctx := TraceEnter("favorite_ints", myint, 23, 64, "hike") 25 defer myctx.TraceExit("no_favorite_ints") 26 } 27 28 func TestAPI(t *testing.T) { 29 confStrings := []string{ 30 "Stats.IPAddr=localhost", 31 "Stats.UDPPort=52184", 32 "Stats.BufferLength=100", 33 "Stats.MaxLatency=1s", 34 "Logging.TraceLevelLogging=logger", 35 "Logging.LogFilePath=/dev/null", 36 "Logging.LogToConsole=false", 37 } 38 39 confMap, err := conf.MakeConfMapFromStrings(confStrings) 40 if err != nil { 41 t.Fatalf("%v", err) 42 } 43 44 err = Up(confMap) 45 if nil != err { 46 tErr := fmt.Sprintf("logger.Up(confMap) failed: %v", err) 47 t.Fatalf(tErr) 48 } 49 50 Tracef("hello there!") 51 Tracef("hello again, %s!", "you") 52 Tracef("%v: %v", utils.GetFnName(), err) 53 Warnf("%v: %v", "IAmTheCaller", "this is the error") 54 err = fmt.Errorf("this is the error") 55 ErrorfWithError(err, "we had an error!") 56 57 testNestedFunc() 58 59 testLogTargets(t) 60 61 testParseLogEntry(t) 62 63 err = Down(confMap) 64 if nil != err { 65 tErr := fmt.Sprintf("logger.Down(confMap) failed: %v", err) 66 t.Fatalf(tErr) 67 } 68 } 69 70 func testLogTargets(t *testing.T) { 71 assert := assert.New(t) 72 73 var targ LogTarget 74 targ.Init(10) 75 76 // targ will get a copy of each log entry (and will live on after this 77 // function returns) 78 AddLogTarget(targ) 79 80 Warnf("Beware the Ides of March") 81 if targ.LogBuf.LogEntries[0] == "" { 82 t.Error("log target didn't receive the logged warning") 83 } 84 if !strings.Contains(targ.LogBuf.LogEntries[0], "the Ides of March") { 85 t.Error("log entry didn't include the 'Ides of March'") 86 } 87 if targ.LogBuf.TotalEntries != 1 { 88 t.Error("log target doesn''t count correctly") 89 } 90 91 Tracef("%s %s", "Hello,", "World") 92 if !strings.Contains(targ.LogBuf.LogEntries[0], "Hello, World") { 93 t.Errorf("log entry '%s' didn't include the 'Hello, World'", targ.LogBuf.LogEntries[0]) 94 } 95 if targ.LogBuf.TotalEntries != 2 { 96 t.Error("log target doesn't count correctly") 97 } 98 99 fields, err := ParseLogEntry(targ.LogBuf.LogEntries[0]) 100 assert.Nil(err, "ParseLogEntry of '%s' should not fail", targ.LogBuf.LogEntries[0]) 101 102 names := []string{ 103 "time", "level", "msg", "goroutine", "package", 104 } 105 for _, nm := range names { 106 if fields[nm] == "" { 107 t.Errorf("unable to parse field '%s' in log entry '%s'", 108 nm, targ.LogBuf.LogEntries[0]) 109 } 110 } 111 } 112 113 func testParseLogEntry(t *testing.T) { 114 assert := assert.New(t) 115 116 var targ LogTarget 117 targ.Init(10) 118 119 // targ will get a copy of each log entry (and will live on after this 120 // function returns) 121 AddLogTarget(targ) 122 123 var ( 124 fields map[string]string 125 names []string 126 err error 127 ) 128 129 // test a trace log entry (logs as level=info 130 Tracef("%s %s", "Hello,", "World") 131 132 fields, err = ParseLogEntry(targ.LogBuf.LogEntries[0]) 133 assert.Nil(err, "ParseLogEntry of '%s' should not fail", targ.LogBuf.LogEntries[0]) 134 135 names = []string{ 136 "time", "level", "msg", "goroutine", "package", 137 } 138 for _, nm := range names { 139 if fields[nm] == "" { 140 t.Errorf("unable to parse field '%s' in log entry '%s'", 141 nm, targ.LogBuf.LogEntries[0]) 142 } 143 } 144 if fields["error"] != "" { 145 t.Errorf("found 'error' field containing '%s' in log entry '%s'", 146 fields["error"], targ.LogBuf.LogEntries[0]) 147 } 148 if fields["level"] != "info" { 149 t.Errorf("'level' field contains '%s' should be 'trace' in entry '%s'", 150 fields["level"], targ.LogBuf.LogEntries[0]) 151 } 152 if fields["msg"] != "Hello, World" { 153 t.Errorf("'msg' field contains '%s' should be 'Hello, World' in entry '%s'", 154 fields["trace"], targ.LogBuf.LogEntries[0]) 155 } 156 157 // test an error entry 158 err = fmt.Errorf("this is the error") 159 ErrorfWithError(err, "we had an error!") 160 161 fields, err = ParseLogEntry(targ.LogBuf.LogEntries[0]) 162 assert.Nil(err, "ParseLogEntry of '%s' should not fail", targ.LogBuf.LogEntries[0]) 163 164 names = []string{ 165 "time", "level", "msg", "goroutine", "package", "error", 166 } 167 for _, nm := range names { 168 if fields[nm] == "" { 169 t.Errorf("unable to parse field '%s' in log entry '%s'", 170 nm, targ.LogBuf.LogEntries[0]) 171 } 172 } 173 if fields["error"] != "this is the error" { 174 t.Errorf("field 'error' contains '%s' should be 'this is the error' in log entry '%s'", 175 fields["error"], targ.LogBuf.LogEntries[0]) 176 } 177 if fields["level"] != "error" { 178 t.Errorf("'level' field contains '%s' should be 'error' in entry '%s'", 179 fields["level"], targ.LogBuf.LogEntries[0]) 180 } 181 if fields["msg"] != "we had an error!" { 182 t.Errorf("'msg' field contains '%s' should be 'we had an error!' in entry '%s'", 183 fields["msg"], targ.LogBuf.LogEntries[0]) 184 } 185 186 msg_in := `When you put "something" in double quotes it means "something else"` 187 msg_out := `When you put \"something\" in double quotes it means \"something else\"` 188 Tracef(msg_in) 189 fields, err = ParseLogEntry(targ.LogBuf.LogEntries[0]) 190 assert.Nil(err, "ParseLogEntry of '%s' should not fail", targ.LogBuf.LogEntries[0]) 191 192 if fields["msg"] != msg_out { 193 t.Errorf("'msg' field contains '%s' should be '%s' in entry '%s'", 194 fields["msg"], msg_out, targ.LogBuf.LogEntries[0]) 195 } 196 197 // Double-plus ungood (both msg and error have nested '"') 198 errmsg_in := `Finding " when you don't expect " can drive you """` 199 errmsg_out := `Finding \" when you don't expect \" can drive you \"\"\"` 200 err = fmt.Errorf(errmsg_in) 201 ErrorfWithError(err, msg_in) 202 203 fields, err = ParseLogEntry(targ.LogBuf.LogEntries[0]) 204 assert.Nil(err, "ParseLogEntry of '%s' should not fail", targ.LogBuf.LogEntries[0]) 205 206 if fields["msg"] != msg_out { 207 t.Errorf("'msg' field contains '%s' should be '%s' in entry '%s'", 208 fields["msg"], msg_out, targ.LogBuf.LogEntries[0]) 209 } 210 if fields["error"] != errmsg_out { 211 t.Errorf("'error' field contains '%s' should be '%s' in entry '%s'", 212 fields["error"], errmsg_out, targ.LogBuf.LogEntries[0]) 213 } 214 }