-
Notifications
You must be signed in to change notification settings - Fork 4
/
alog_test.go
125 lines (105 loc) · 2.74 KB
/
alog_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
package alog
import (
"bytes"
"context"
"fmt"
"path/filepath"
"testing"
"time"
)
func Example_levels() {
ctx := context.Background()
l := New(WithEmitter(EmitterFunc(func(ctx context.Context, e *Entry) {
for _, p := range e.Tags {
if p[0] != "level" {
continue
}
switch p[1] {
case "error":
fmt.Println("ERROR", e.Tags, e.Msg)
fallthrough
case "info":
fallthrough
case "debug":
return
}
}
})))
error := AddTags(ctx, "level", "error")
info := AddTags(ctx, "level", "info")
debug := AddTags(ctx, "level", "debug")
l.Print(debug, "test")
l.Print(info, "test")
l.Print(error, "test")
// Output:
// ERROR [[level error]] test
}
func ExampleWithEmitter() {
dumper := EmitterFunc(func(ctx context.Context, e *Entry) {
fmt.Printf("%v %s\n", e.Tags, e.Msg)
})
ctx := context.Background()
l := New(WithEmitter(dumper))
ctx = AddTags(ctx, "allthese", "tags")
l.Print(ctx, "test")
// Output:
// [[allthese tags]] test
}
func ExampleWithCaller() {
dumper := EmitterFunc(func(ctx context.Context, e *Entry) {
fmt.Printf("%s:%d %s\n", filepath.Base(e.File), e.Line, e.Msg)
})
ctx := context.Background()
l := New(WithEmitter(dumper), WithCaller())
l.Print(ctx, "test")
// Output:
// alog_test.go:61 test
}
func TestOverrideTimestamp(t *testing.T) {
buf := &bytes.Buffer{}
dumper := EmitterFunc(func(ctx context.Context, e *Entry) {
fmt.Fprintf(buf, "%s %s\n", e.Time.Format(time.RFC3339), e.Msg)
})
ctx := context.Background()
l := New(WithEmitter(dumper), OverrideTimestamp(func() time.Time { return time.Time{} }))
l.Print(ctx, "test")
want := "0001-01-01T00:00:00Z test\n"
got := buf.String()
if got != want {
t.Fatalf("want: %#q, got: %#q", want, got)
}
}
func TestNilOK(t *testing.T) {
t.Parallel()
var l *Logger
ctx := context.Background()
l.Print(ctx, "this shouldn't explode")
}
func TestIgnoredTag(t *testing.T) {
t.Parallel()
buf := &bytes.Buffer{}
want := "[[a b]] test\n"
l := New(WithEmitter(EmitterFunc(func(ctx context.Context, e *Entry) {
fmt.Fprintf(buf, "%v %s\n", e.Tags, e.Msg)
})))
ctx := AddTags(context.Background(), "a", "b", "unpaired")
l.Print(ctx, "test")
if got := buf.String(); want != got {
t.Fatalf("want: %#q, got: %#q", want, got)
}
}
func TestStdLogger(t *testing.T) {
buf := &bytes.Buffer{}
dumper := EmitterFunc(func(ctx context.Context, e *Entry) {
fmt.Fprintf(buf, "%s %v %s\n", e.Time.Format(time.RFC3339), e.Tags, e.Msg)
})
l := New(WithEmitter(dumper), OverrideTimestamp(func() time.Time { return time.Time{} }))
ctx := AddTags(context.Background(), "a", "b")
sl := l.StdLogger(ctx)
sl.Printf("test")
want := "0001-01-01T00:00:00Z [[a b]] test\n"
got := buf.String()
if got != want {
t.Fatalf("want: %#q, got: %#q", want, got)
}
}