-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
pkg_test.go
81 lines (65 loc) · 1.63 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
package log_test
import (
"errors"
"testing"
"github.com/apex/log"
"github.com/apex/log/handlers/memory"
"github.com/stretchr/testify/assert"
)
type Pet struct {
Name string
Age int
}
func (p *Pet) Fields() log.Fields {
return log.Fields{
"name": p.Name,
"age": p.Age,
}
}
func TestInfo(t *testing.T) {
h := memory.New()
log.SetHandler(h)
log.Infof("logged in %s", "Tobi")
e := h.Entries[0]
assert.Equal(t, e.Message, "logged in Tobi")
assert.Equal(t, e.Level, log.InfoLevel)
}
func TestFielder(t *testing.T) {
h := memory.New()
log.SetHandler(h)
pet := &Pet{"Tobi", 3}
log.WithFields(pet).Info("add pet")
e := h.Entries[0]
assert.Equal(t, log.Fields{"name": "Tobi", "age": 3}, e.Fields)
}
// 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")
}
// Trace can be used to simplify logging of start and completion events,
// for example an upload which may fail.
func Example_trace() {
fn := func() (err error) {
defer log.Trace("upload").Stop(&err)
return
}
fn()
return
}