Debian Codegraph @

compact_printer_test.go

raw symbols SCIP
1 package jl
2
3 import (
4 "bytes"
5 "encoding/json"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 )
11
12 func TestCompactPrinter_PrintNonJSON(t *testing.T) {
13 raw := "hello world"
14 buf := &bytes.Buffer{}
15 printer := NewCompactPrinter(buf)
16 entry := &Entry{
17 Raw: []byte(raw),
18 }
19 printer.Print(entry)
20 assert.Equal(t, raw+"\n", buf.String())
21 }
22
23 func TestCompactPrinter_Print(t *testing.T) {
24 tests := []struct {
25 name string
26 json string
27 formatted string
28 }{{
29 name: "basic",
30 json: `{"timestamp":"2019-01-01 15:23:45","level":"INFO","thread":"truck-manager","traceId":"123125f435d657a1","logger":"TruckRepairServiceOverlordManager","message":"There are 7 more trucks in the garage to fix. Get to work."}`,
31 formatted: "INFO 2019-01-01 15:23:45 [truck-manager] TruckRepa…ordManager| 123125f435d657a1| There are 7 more trucks in the garage to fix. Get to work.\n",
32 }, {
33 name: "exception",
34 json: `{"timestamp":"2019-01-01 15:34:45","level":"ERROR","thread":"repair-worker-2","traceId":"123125f435d657a1","logger":"TruckRepairMinion","message":"Truck 5 has is really broken! I'm need parts, waiting till they come."}`,
35 formatted: `ERRO 2019-01-01 15:34:45 [repair-worker-2] TruckRepairMinion| 123125f435d657a1| Truck 5 has is really broken! I'm need parts, waiting till they come.
36 `}, {
37 name: "logrus_pgk_error",
38 json: `{"timestamp":"2019-01-01 15:23:45","level":"error","message":"an error occurred","traceId":"123125f435d657a1","error":"BOOM!","stack":"github.com/pkg/errors_test.fn\n\t/home/dfc/src/github.com/pkg/errors/example_test.go:47\ngithub.com/pkg/errors_test.Example_stackTrace\n\t/home/dfc/src/github.com/pkg/errors/example_test.go:127\n"}`,
39 formatted: `ERRO 2019-01-01 15:23:45 123125f435d657a1| an error occurred
40 BOOM!
41 github.com/pkg/errors_test.fn
42 /home/dfc/src/github.com/pkg/errors/example_test.go:47
43 github.com/pkg/errors_test.Example_stackTrace
44 /home/dfc/src/github.com/pkg/errors/example_test.go:127
45 `,
46 }}
47 for _, test := range tests {
48 t.Run(test.name, func(t *testing.T) {
49 buf := &bytes.Buffer{}
50 printer := NewCompactPrinter(buf)
51 printer.DisableColor = true
52 entry := &Entry{
53 Raw: []byte(test.json),
54 }
55 require.NoError(t, json.Unmarshal([]byte(test.json), &entry.Partials))
56 printer.Print(entry)
57 assert.Equal(t, test.formatted, buf.String())
58 })
59 }
60 }
61
62 func TestCompactPrinter_PrintWithColors(t *testing.T) {
63 var logs = []string{
64 `{"timestamp":"2019-01-01 15:24:45","level":"info","thread":"repair-worker-1","logger":"truckrepairminion","message":"fixing truck 1, it's got a broken axle"}`,
65 `{"timestamp":"2019-01-01 15:25:45","level":"info","thread":"repair-worker-2","logger":"truckrepairminion","message":"fixing truck 2, it's got a broken axle"}`,
66 }
67 var formatted = []string{
68 "\x1b[32mINFO\x1b[0m 2019-01-01 15:24:45 \x1b[32m[repair-worker-1] \x1b[0m \x1b[32m truckrepairminion|\x1b[0m fixing truck 1, it's got a broken axle\n",
69 "\x1b[32mINFO\x1b[0m 2019-01-01 15:25:45 \x1b[33m[repair-worker-2] \x1b[0m \x1b[32m truckrepairminion|\x1b[0m fixing truck 2, it's got a broken axle\n",
70 }
71 printer := NewCompactPrinter(nil)
72 for i, log := range logs {
73 buf := &bytes.Buffer{}
74 printer.Out = buf
75 entry := &Entry{
76 Raw: []byte(log),
77 }
78 require.NoError(t, json.Unmarshal([]byte(log), &entry.Partials))
79 printer.Print(entry)
80 assert.Equal(t, formatted[i], buf.String())
81 }
82 }