forked from caarlos0/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg_test.go
137 lines (119 loc) · 2.77 KB
/
pkg_test.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package log_test
import (
"bytes"
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/caarlos0/log"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
)
func init() {
lipgloss.SetColorProfile(termenv.ANSI256)
}
type Pet struct {
Name string
Age int
}
func (p *Pet) Fields() log.Fields {
return log.Fields{
"name": p.Name,
"age": p.Age,
}
}
func TestRootLogOptions(t *testing.T) {
var out bytes.Buffer
log.Log = log.New(&out)
log.SetLevel(log.DebugLevel)
log.SetLevelFromString("info")
log.WithError(fmt.Errorf("here")).Info("a")
log.Debug("debug")
log.Debugf("warn %d", 1)
log.Info("info")
log.Infof("warn %d", 1)
log.Warn("warn")
log.Warnf("warn %d", 1)
log.Error("error")
log.Errorf("warn %d", 1)
log.WithField("foo", "bar").Info("foo")
log.IncreasePadding()
log.Info("increased")
log.WithoutPadding().Info("without padding")
log.Info("increased")
log.ResetPadding()
pet := &Pet{"Tobi", 3}
log.WithFields(pet).Info("add pet")
requireEqualOutput(t, out.Bytes())
}
func TestRace(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 9999; i++ {
wg.Add(1)
go func() {
defer wg.Done()
log.Infof("a")
}()
}
wg.Wait()
}
// Unstructured logging is supported, but not recommended since it is hard to query.
func Example_unstructured() {
log.Infof("%s logged in", "Tobi")
}
// Structured logging is supported with fields, and is recommended over the formatted message variants.
func Example_structured() {
log.WithField("user", "Tobo").Info("logged in")
}
// Errors are passed to WithError(), populating the "error" field.
func Example_errors() {
err := errors.New("boom")
log.WithError(err).Error("upload failed")
}
// Multiple fields can be set, via chaining, or WithFields().
func Example_multipleFields() {
log.WithFields(log.Fields{
"user": "Tobi",
"file": "sloth.png",
"type": "image/png",
}).Info("upload")
}
var update = flag.Bool("update", false, "update .golden files")
func requireEqualOutput(tb testing.TB, in []byte) {
tb.Helper()
bts := useLinuxEOL(in)
golden := "testdata/" + tb.Name() + ".golden"
if *update {
if err := os.MkdirAll(filepath.Dir(golden), 0o755); err != nil {
tb.Fatal(err)
}
if err := os.WriteFile(golden, bts, 0o600); err != nil {
tb.Fatal(err)
}
}
gbts, err := os.ReadFile(golden)
if err != nil {
tb.Fatal(err)
}
gbts = useLinuxEOL(gbts)
if !bytes.Equal(bts, gbts) {
sg := format(string(gbts))
so := format(string(bts))
tb.Fatalf("output do not match:\ngot:\n%s\n\nexpected:\n%s\n\n", so, sg)
}
}
func useLinuxEOL(bts []byte) []byte {
return bytes.ReplaceAll(bts, []byte("\r\n"), []byte("\n"))
}
func format(str string) string {
return strings.NewReplacer(
"\x1b", "\\x1b",
"\n", "\\n",
"\r", "\\r",
"\t", "\\t",
).Replace(str)
}