File: logfmt_printer_test.go

package info (click to toggle)
golang-github-mightyguava-jl 0.1.0%2Bgit20220705%2B8771236337c6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,112 kB
  • sloc: makefile: 5
file content (41 lines) | stat: -rw-r--r-- 1,604 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package jl

import (
	"bytes"
	"encoding/json"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestLogfmtPrinter_Print(t *testing.T) {
	tests := []struct {
		name      string
		json      string
		formatted string
		color     bool
	}{{
		name:      "basic",
		json:      `{"timestamp":"2019-01-01 15:23:45","level":"INFO","thread":"truck-manager","logger":"TruckRepairServiceOverlordManager","message":"There are 7 more trucks in the garage to fix. Get to work."}`,
		formatted: "timestamp=2019-01-01 15:23:45 level=INFO thread=truck-manager logger=TruckRepairServiceOverlordManager message=There are 7 more trucks in the garage to fix. Get to work.\n",
	}, {
		name:      "color",
		json:      `{"timestamp":"2019-01-01 15:23:45","level":"INFO","thread":"truck-manager","logger":"TruckRepairServiceOverlordManager","message":"There are 7 more trucks in the garage to fix. Get to work."}`,
		color:     true,
		formatted: "\x1b[32mtimestamp\x1b[0m=2019-01-01 15:23:45 \x1b[32mlevel\x1b[0m=INFO \x1b[32mthread\x1b[0m=truck-manager \x1b[32mlogger\x1b[0m=TruckRepairServiceOverlordManager \x1b[32mmessage\x1b[0m=There are 7 more trucks in the garage to fix. Get to work.\n",
	}}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			buf := &bytes.Buffer{}
			printer := NewLogfmtPrinter(buf)
			printer.DisableColor = !test.color
			entry := &Entry{
				Raw: []byte(test.json),
			}
			require.NoError(t, json.Unmarshal([]byte(test.json), &entry.Partials))
			printer.Print(entry)
			assert.Equal(t, test.formatted, buf.String())
		})
	}
}