-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_test.go
More file actions
68 lines (52 loc) · 1.05 KB
/
log_test.go
File metadata and controls
68 lines (52 loc) · 1.05 KB
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
package teapot
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"testing"
)
func TestLogger_Levels(t *testing.T) {
var buf bytes.Buffer
l := New(
WithLevel(INFO),
WithWriter(&buf),
)
l.printf("hello", DEBUG)
if buf.Len() > 0 {
t.Fatal("expected empty buffer")
}
l.printf("hello", INFO)
if buf.Len() == 0 {
t.Fatal("expected non empty buffer")
}
}
func TestLogger_JSON(t *testing.T) {
var buf bytes.Buffer
l := New(
WithLevel(INFO),
WithWriter(&buf),
)
l.Info("hello",
String("user", "alice"),
StringSlice("x", []string{"hello", "world"}),
)
fmt.Println(buf.String())
var result map[string]interface{}
if err := json.Unmarshal(buf.Bytes(), &result); err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
if result["user"] != "alice" {
t.Fatalf("unexpected user value %s expected alice", result["user"])
}
}
func TestLogger_Error(t *testing.T) {
var buf bytes.Buffer
l := New(
WithLevel(ERROR),
WithWriter(&buf),
)
e := errors.New("something happened")
l.Error("this is bad", Error(e))
fmt.Println(buf.String())
}