| 1 |
package humanlog |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
"time" |
| 6 |
|
| 7 |
"github.com/humanlogio/humanlog/internal/pkg/model" |
| 8 |
) |
| 9 |
|
| 10 |
var logLinesByLevel = map[string][]byte{ |
| 11 |
"DEBUG": []byte(`2021-02-05T12:41:48.053-0700 DEBUG zapper/zapper.go:18 some message 1 {"rand_index": 1}`), |
| 12 |
"ERROR": []byte(`2021-02-05T12:41:49.059-0700 ERROR zapper/zapper.go:18 some message 2 {"rand_index": 3}`), |
| 13 |
"FATAL": []byte(`2021-02-05T15:45:04.425-0700 FATAL zapper/zapper.go:18 some message 5 {"rand_index": 11}`), |
| 14 |
"INFO": []byte(`2021-02-05T12:41:50.064-0700 INFO zapper/zapper.go:18 some message 3 {"rand_index": 5}`), |
| 15 |
"WARN": []byte(`2021-02-05T12:41:51.069-0700 WARN zapper/zapper.go:18 some message 4 {"rand_index": 7}`), |
| 16 |
} |
| 17 |
|
| 18 |
func Test_zapDevLogsPrefixRe(t *testing.T) { |
| 19 |
tests := []struct { |
| 20 |
name string |
| 21 |
logLine []byte |
| 22 |
wantTS string |
| 23 |
wantLevel string |
| 24 |
wantLocation string |
| 25 |
wantMessage string |
| 26 |
wantJSON string |
| 27 |
}{ |
| 28 |
{ |
| 29 |
name: "debug message", |
| 30 |
|
| 31 |
logLine: logLinesByLevel["DEBUG"], |
| 32 |
|
| 33 |
wantTS: "2021-02-05T12:41:48.053-0700", |
| 34 |
wantLevel: "DEBUG", |
| 35 |
wantLocation: "zapper/zapper.go:18", |
| 36 |
wantMessage: "some message 1", |
| 37 |
wantJSON: `{"rand_index": 1}`, |
| 38 |
}, |
| 39 |
{ |
| 40 |
name: "error message", |
| 41 |
|
| 42 |
logLine: logLinesByLevel["ERROR"], |
| 43 |
|
| 44 |
wantTS: "2021-02-05T12:41:49.059-0700", |
| 45 |
wantLevel: "ERROR", |
| 46 |
wantLocation: "zapper/zapper.go:18", |
| 47 |
wantMessage: "some message 2", |
| 48 |
wantJSON: `{"rand_index": 3}`, |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "fatal message", |
| 52 |
logLine: logLinesByLevel["FATAL"], |
| 53 |
wantTS: "2021-02-05T15:45:04.425-0700", |
| 54 |
wantLevel: "FATAL", |
| 55 |
wantLocation: "zapper/zapper.go:18", |
| 56 |
wantMessage: "some message 5", |
| 57 |
wantJSON: `{"rand_index": 11}`, |
| 58 |
}, |
| 59 |
{ |
| 60 |
name: "info message", |
| 61 |
|
| 62 |
logLine: logLinesByLevel["INFO"], |
| 63 |
|
| 64 |
wantTS: "2021-02-05T12:41:50.064-0700", |
| 65 |
wantLevel: "INFO", |
| 66 |
wantLocation: "zapper/zapper.go:18", |
| 67 |
wantMessage: "some message 3", |
| 68 |
wantJSON: `{"rand_index": 5}`, |
| 69 |
}, |
| 70 |
{ |
| 71 |
|
| 72 |
name: "warning message", |
| 73 |
|
| 74 |
logLine: logLinesByLevel["WARN"], |
| 75 |
|
| 76 |
wantTS: "2021-02-05T12:41:51.069-0700", |
| 77 |
wantLevel: "WARN", |
| 78 |
wantLocation: "zapper/zapper.go:18", |
| 79 |
wantMessage: "some message 4", |
| 80 |
wantJSON: `{"rand_index": 7}`, |
| 81 |
}, |
| 82 |
} |
| 83 |
|
| 84 |
for _, test := range tests { |
| 85 |
t.Run(test.name, func(t *testing.T) { |
| 86 |
matches := zapDevLogsPrefixRe.FindSubmatch(test.logLine) |
| 87 |
if matches != nil { |
| 88 |
result := make(map[string]string) |
| 89 |
for i, name := range zapDevLogsPrefixRe.SubexpNames() { |
| 90 |
if i != 0 && name != "" { |
| 91 |
result[name] = string(matches[i]) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
if result["timestamp"] != test.wantTS { |
| 96 |
t.Errorf("want %q, got %q, want != got", test.wantTS, result["timestamp"]) |
| 97 |
} |
| 98 |
|
| 99 |
if result["level"] != test.wantLevel { |
| 100 |
t.Errorf("want %q, got %q, want != got", test.wantLevel, result["level"]) |
| 101 |
} |
| 102 |
|
| 103 |
if result["location"] != test.wantLocation { |
| 104 |
t.Errorf("want %q, got %q, want != got", test.wantLocation, result["location"]) |
| 105 |
} |
| 106 |
|
| 107 |
if result["message"] != test.wantMessage { |
| 108 |
t.Errorf("want %q, got %q, want != got", test.wantMessage, result["message"]) |
| 109 |
} |
| 110 |
|
| 111 |
if result["jsonbody"] != test.wantJSON { |
| 112 |
t.Errorf("want %q, got %q, want != got", test.wantJSON, result["jsonbody"]) |
| 113 |
} |
| 114 |
} else { |
| 115 |
t.Errorf("regular expression did not match log line") |
| 116 |
} |
| 117 |
}) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func Test_tryZapDevPrefix(t *testing.T) { |
| 122 |
tests := []struct { |
| 123 |
name string |
| 124 |
logLine []byte |
| 125 |
wantMatch bool |
| 126 |
|
| 127 |
wantTime time.Time |
| 128 |
wantLevel string |
| 129 |
wantLocation string |
| 130 |
wantMessage string |
| 131 |
|
| 132 |
wantExtraContext string |
| 133 |
}{ |
| 134 |
{ |
| 135 |
name: "no match", |
| 136 |
|
| 137 |
logLine: []byte("that's no good"), |
| 138 |
|
| 139 |
wantMatch: false, |
| 140 |
}, |
| 141 |
{ |
| 142 |
name: "debug message", |
| 143 |
|
| 144 |
logLine: logLinesByLevel["DEBUG"], |
| 145 |
|
| 146 |
wantMatch: true, |
| 147 |
wantTime: time.Date(2021, 2, 5, 12, 41, 48, 53e6, time.FixedZone("", -7*3600)), |
| 148 |
wantLevel: "debug", |
| 149 |
wantLocation: "zapper/zapper.go:18", |
| 150 |
wantMessage: "some message 1", |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "error message with caller info", |
| 154 |
|
| 155 |
logLine: logLinesByLevel["ERROR"], |
| 156 |
|
| 157 |
wantMatch: true, |
| 158 |
wantTime: time.Date(2021, 2, 5, 12, 41, 49, 59e6, time.FixedZone("", -7*3600)), |
| 159 |
wantLevel: "error", |
| 160 |
wantLocation: "zapper/zapper.go:18", |
| 161 |
wantMessage: "some message 2", |
| 162 |
}, |
| 163 |
{ |
| 164 |
name: "fatal message with caller info and exit status", |
| 165 |
|
| 166 |
logLine: logLinesByLevel["FATAL"], |
| 167 |
|
| 168 |
wantMatch: true, |
| 169 |
wantTime: time.Date(2021, 2, 5, 15, 45, 4, 425e6, time.FixedZone("", -7*3600)), |
| 170 |
wantLevel: "fatal", |
| 171 |
wantLocation: "zapper/zapper.go:18", |
| 172 |
wantMessage: "some message 5", |
| 173 |
}, |
| 174 |
{ |
| 175 |
name: "info message", |
| 176 |
|
| 177 |
logLine: logLinesByLevel["INFO"], |
| 178 |
|
| 179 |
wantMatch: true, |
| 180 |
wantTime: time.Date(2021, 2, 5, 12, 41, 50, 64e6, time.FixedZone("", -7*3600)), |
| 181 |
wantLevel: "info", |
| 182 |
wantLocation: "zapper/zapper.go:18", |
| 183 |
wantMessage: "some message 3", |
| 184 |
}, |
| 185 |
{ |
| 186 |
|
| 187 |
name: "warning message with caller info", |
| 188 |
|
| 189 |
logLine: logLinesByLevel["WARN"], |
| 190 |
|
| 191 |
wantMatch: true, |
| 192 |
wantTime: time.Date(2021, 2, 5, 12, 41, 51, 69e6, time.FixedZone("", -7*3600)), |
| 193 |
wantLevel: "warn", |
| 194 |
wantLocation: "zapper/zapper.go:18", |
| 195 |
wantMessage: "some message 4", |
| 196 |
}, |
| 197 |
} |
| 198 |
|
| 199 |
for _, test := range tests { |
| 200 |
t.Run(test.name, func(t *testing.T) { |
| 201 |
ev := new(model.Structured) |
| 202 |
m := tryZapDevPrefix(test.logLine, ev, &JSONHandler{Opts: DefaultOptions()}) |
| 203 |
|
| 204 |
if m != test.wantMatch { |
| 205 |
t.Error("expected the prefix to match, it did not") |
| 206 |
} |
| 207 |
|
| 208 |
if !test.wantMatch { |
| 209 |
return |
| 210 |
} |
| 211 |
|
| 212 |
if !test.wantTime.Equal(ev.Time) { |
| 213 |
t.Errorf("want %v, got %v; want != got", test.wantTime, ev.Time) |
| 214 |
} |
| 215 |
if ev.Level != test.wantLevel { |
| 216 |
t.Errorf("want %q, got %q; want != got", test.wantLevel, ev.Level) |
| 217 |
} |
| 218 |
if ev.Msg != test.wantMessage { |
| 219 |
t.Errorf("want %q, got %q; want != got", test.wantMessage, ev.Msg) |
| 220 |
} |
| 221 |
|
| 222 |
if findFieldValue(ev, "caller") != test.wantLocation { |
| 223 |
t.Errorf("want %q, got %q; want != got", test.wantLocation, findFieldValue(ev, "caller")) |
| 224 |
} |
| 225 |
}) |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
func findFieldValue(ev *model.Structured, field string) string { |
| 230 |
for _, kv := range ev.KVs { |
| 231 |
if kv.Key == field { |
| 232 |
return kv.Value |
| 233 |
} |
| 234 |
} |
| 235 |
return "" |
| 236 |
} |
| 237 |
|
| 238 |
var dcLogLinesByLevel = map[string][]byte{ |
| 239 |
"DEBUG": []byte("2021-02-06T22:55:22.004Z\tDEBUG\tzapper/zapper.go:17\tsome message 1\t{\"rand_index\": 1}"), |
| 240 |
"ERROR": []byte("2021-02-06T22:55:22.008Z\tERROR\tzapper/zapper.go:17\tsome message 2\t{\"rand_index\": 2}"), |
| 241 |
"FATAL": []byte("2021-02-06T22:55:22.009Z\tFATAL\tzapper/zapper.go:17\tsome message 5\t{\"rand_index\": 1}"), |
| 242 |
"INFO": []byte("2021-02-06T22:55:22.009Z\tINFO\tzapper/zapper.go:17\tsome message 3\t{\"rand_index\": 2}"), |
| 243 |
"WARN": []byte("2021-02-06T22:55:22.009Z\tWARN\tzapper/zapper.go:17\tsome message 4\t{\"rand_index\": 4}"), |
| 244 |
} |
| 245 |
|
| 246 |
func Test_zapDCDevLogsPrefixRe(t *testing.T) { |
| 247 |
tests := []struct { |
| 248 |
name string |
| 249 |
logLine []byte |
| 250 |
wantTS string |
| 251 |
wantLevel string |
| 252 |
wantLocation string |
| 253 |
wantMessage string |
| 254 |
wantJSON string |
| 255 |
}{ |
| 256 |
{ |
| 257 |
name: "debug message", |
| 258 |
|
| 259 |
logLine: dcLogLinesByLevel["DEBUG"], |
| 260 |
|
| 261 |
wantTS: "2021-02-06T22:55:22.004Z", |
| 262 |
wantLevel: "DEBUG", |
| 263 |
wantLocation: "zapper/zapper.go:17", |
| 264 |
wantMessage: "some message 1", |
| 265 |
wantJSON: `{"rand_index": 1}`, |
| 266 |
}, |
| 267 |
{ |
| 268 |
name: "error message", |
| 269 |
|
| 270 |
logLine: dcLogLinesByLevel["ERROR"], |
| 271 |
|
| 272 |
wantTS: "2021-02-06T22:55:22.008Z", |
| 273 |
wantLevel: "ERROR", |
| 274 |
wantLocation: "zapper/zapper.go:17", |
| 275 |
wantMessage: "some message 2", |
| 276 |
wantJSON: `{"rand_index": 2}`, |
| 277 |
}, |
| 278 |
{ |
| 279 |
name: "fatal message", |
| 280 |
|
| 281 |
logLine: dcLogLinesByLevel["FATAL"], |
| 282 |
|
| 283 |
wantTS: "2021-02-06T22:55:22.009Z", |
| 284 |
wantLevel: "FATAL", |
| 285 |
wantLocation: "zapper/zapper.go:17", |
| 286 |
wantMessage: "some message 5", |
| 287 |
wantJSON: `{"rand_index": 1}`, |
| 288 |
}, |
| 289 |
{ |
| 290 |
name: "info message", |
| 291 |
|
| 292 |
logLine: dcLogLinesByLevel["INFO"], |
| 293 |
|
| 294 |
wantTS: "2021-02-06T22:55:22.009Z", |
| 295 |
wantLevel: "INFO", |
| 296 |
wantLocation: "zapper/zapper.go:17", |
| 297 |
wantMessage: "some message 3", |
| 298 |
wantJSON: `{"rand_index": 2}`, |
| 299 |
}, |
| 300 |
{ |
| 301 |
name: "warn message", |
| 302 |
|
| 303 |
logLine: dcLogLinesByLevel["WARN"], |
| 304 |
|
| 305 |
wantTS: "2021-02-06T22:55:22.009Z", |
| 306 |
wantLevel: "WARN", |
| 307 |
wantLocation: "zapper/zapper.go:17", |
| 308 |
wantMessage: "some message 4", |
| 309 |
wantJSON: `{"rand_index": 4}`, |
| 310 |
}, |
| 311 |
} |
| 312 |
|
| 313 |
for _, test := range tests { |
| 314 |
t.Run(test.name, func(t *testing.T) { |
| 315 |
matches := zapDevDCLogsPrefixRe.FindSubmatch(test.logLine) |
| 316 |
if matches != nil { |
| 317 |
result := make(map[string]string) |
| 318 |
for i, name := range zapDevLogsPrefixRe.SubexpNames() { |
| 319 |
if i != 0 && name != "" { |
| 320 |
result[name] = string(matches[i]) |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
if result["timestamp"] != test.wantTS { |
| 325 |
t.Errorf("want %q, got %q, want != got", test.wantTS, result["timestamp"]) |
| 326 |
} |
| 327 |
|
| 328 |
if result["level"] != test.wantLevel { |
| 329 |
t.Errorf("want %q, got %q, want != got", test.wantLevel, result["level"]) |
| 330 |
} |
| 331 |
|
| 332 |
if result["location"] != test.wantLocation { |
| 333 |
t.Errorf("want %q, got %q, want != got", test.wantLocation, result["location"]) |
| 334 |
} |
| 335 |
|
| 336 |
if result["message"] != test.wantMessage { |
| 337 |
t.Errorf("want %q, got %q, want != got", test.wantMessage, result["message"]) |
| 338 |
} |
| 339 |
|
| 340 |
if result["jsonbody"] != test.wantJSON { |
| 341 |
t.Errorf("want %q, got %q, want != got", test.wantJSON, result["jsonbody"]) |
| 342 |
} |
| 343 |
} else { |
| 344 |
t.Errorf("regular expression did not match log line") |
| 345 |
} |
| 346 |
}) |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
func Test_tryZapDevDCPrefix(t *testing.T) { |
| 351 |
tests := []struct { |
| 352 |
name string |
| 353 |
logLine []byte |
| 354 |
wantMatch bool |
| 355 |
|
| 356 |
wantTime time.Time |
| 357 |
wantLevel string |
| 358 |
wantLocation string |
| 359 |
wantMessage string |
| 360 |
|
| 361 |
wantExtraContext string |
| 362 |
}{ |
| 363 |
{ |
| 364 |
name: "no match", |
| 365 |
|
| 366 |
logLine: []byte("that's no good"), |
| 367 |
|
| 368 |
wantMatch: false, |
| 369 |
}, |
| 370 |
{ |
| 371 |
name: "debug message", |
| 372 |
|
| 373 |
logLine: dcLogLinesByLevel["DEBUG"], |
| 374 |
|
| 375 |
wantMatch: true, |
| 376 |
wantTime: time.Date(2021, 2, 6, 22, 55, 22, 4e6, time.UTC), |
| 377 |
wantLevel: "debug", |
| 378 |
wantLocation: "zapper/zapper.go:17", |
| 379 |
wantMessage: "some message 1", |
| 380 |
}, |
| 381 |
{ |
| 382 |
name: "error message with caller info", |
| 383 |
|
| 384 |
logLine: dcLogLinesByLevel["ERROR"], |
| 385 |
|
| 386 |
wantMatch: true, |
| 387 |
wantTime: time.Date(2021, 2, 6, 22, 55, 22, 8e6, time.UTC), |
| 388 |
wantLevel: "error", |
| 389 |
wantLocation: "zapper/zapper.go:17", |
| 390 |
wantMessage: "some message 2", |
| 391 |
}, |
| 392 |
{ |
| 393 |
name: "fatal message with caller info and exit status", |
| 394 |
|
| 395 |
logLine: dcLogLinesByLevel["FATAL"], |
| 396 |
|
| 397 |
wantMatch: true, |
| 398 |
|
| 399 |
wantTime: time.Date(2021, 2, 6, 22, 55, 22, 9e6, time.UTC), |
| 400 |
wantLevel: "fatal", |
| 401 |
wantLocation: "zapper/zapper.go:17", |
| 402 |
wantMessage: "some message 5", |
| 403 |
}, |
| 404 |
{ |
| 405 |
name: "info message", |
| 406 |
|
| 407 |
logLine: dcLogLinesByLevel["INFO"], |
| 408 |
|
| 409 |
wantMatch: true, |
| 410 |
wantTime: time.Date(2021, 2, 6, 22, 55, 22, 9e6, time.UTC), |
| 411 |
wantLevel: "info", |
| 412 |
wantLocation: "zapper/zapper.go:17", |
| 413 |
wantMessage: "some message 3", |
| 414 |
}, |
| 415 |
{ |
| 416 |
|
| 417 |
name: "warning message with caller info", |
| 418 |
|
| 419 |
logLine: dcLogLinesByLevel["WARN"], |
| 420 |
|
| 421 |
wantMatch: true, |
| 422 |
wantTime: time.Date(2021, 2, 6, 22, 55, 22, 9e6, time.UTC), |
| 423 |
wantLevel: "warn", |
| 424 |
wantLocation: "zapper/zapper.go:17", |
| 425 |
wantMessage: "some message 4", |
| 426 |
}, |
| 427 |
} |
| 428 |
|
| 429 |
for _, test := range tests { |
| 430 |
t.Run(test.name, func(t *testing.T) { |
| 431 |
ev := new(model.Structured) |
| 432 |
m := tryZapDevDCPrefix(test.logLine, ev, &JSONHandler{Opts: DefaultOptions()}) |
| 433 |
|
| 434 |
if m != test.wantMatch { |
| 435 |
t.Error("expected the prefix to match, it did not") |
| 436 |
} |
| 437 |
|
| 438 |
if !test.wantMatch { |
| 439 |
return |
| 440 |
} |
| 441 |
|
| 442 |
if !test.wantTime.Equal(ev.Time) { |
| 443 |
t.Errorf("want %v, got %v; want != got", test.wantTime, ev.Time) |
| 444 |
} |
| 445 |
if ev.Level != test.wantLevel { |
| 446 |
t.Errorf("want %q, got %q; want != got", test.wantLevel, ev.Level) |
| 447 |
} |
| 448 |
if ev.Msg != test.wantMessage { |
| 449 |
t.Errorf("want %q, got %q; want != got", test.wantMessage, ev.Msg) |
| 450 |
} |
| 451 |
|
| 452 |
if findFieldValue(ev, "caller") != test.wantLocation { |
| 453 |
t.Errorf("want %q, got %q; want != got", test.wantLocation, findFieldValue(ev, "caller")) |
| 454 |
} |
| 455 |
}) |
| 456 |
} |
| 457 |
} |