-
Notifications
You must be signed in to change notification settings - Fork 1
/
servicelog_test.go
79 lines (66 loc) · 2.02 KB
/
servicelog_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
package apexovernsq
import (
"fmt"
"os"
"testing"
"github.com/apex/log"
"github.com/apex/log/handlers/logfmt"
"github.com/apex/log/handlers/memory"
)
const expectedServiceName string = "apexovernsq.test"
func TestProcessName(t *testing.T) {
result := processName()
if result != expectedServiceName {
t.Fatalf("Expected %q, got %q", expectedServiceName, result)
}
}
func TestNewServiceLogContext(t *testing.T) {
handler := memory.New()
log.SetHandler(handler)
ctx := NewApexLogServiceContext()
ctx.Info("Hello")
entry := handler.Entries[0]
if len(entry.Fields) != 3 {
t.Fatalf("Expected 3 fields, got %d", len(entry.Fields))
}
serviceName := entry.Fields.Get("service")
if serviceName == nil {
t.Fatalf("No serviceName field in Entry")
}
if serviceName != expectedServiceName {
t.Errorf("Expected %q, got %s", expectedServiceName, serviceName)
}
pid := entry.Fields.Get("pid")
expectedPid := fmt.Sprintf("%d", os.Getpid())
if pid != expectedPid {
t.Errorf("Expected %s, got %s", expectedPid, pid)
}
hostname := entry.Fields.Get("hostname")
expectedHostname, _ := os.Hostname()
if hostname != expectedHostname {
t.Errorf("Expected %q, got %q", expectedHostname, hostname)
}
}
// If we create a new service log context from an existing context,
// we'll use it's Logger not the default one.
func TestNewServiceLogContextWithHandler(t *testing.T) {
mem := memory.New()
lf := logfmt.New(os.Stdout)
log.SetHandler(lf)
ctx2 := NewApexLogServiceContextWithHandler(mem)
_, ok := ctx2.Logger.Handler.(*logfmt.Handler)
if ok {
t.Fatal("got logfmt handler expected memory handler")
}
}
// The ServiceFilterApexLogHandler will let all entries through when the filter is nil.
func TestServiceFilterApexLogHandlerNoFilter(t *testing.T) {
mem := memory.New()
handler := NewApexLogServiceFilterHandler(mem, nil)
log.SetHandler(handler)
log.WithFields(log.Fields{"service": "test"}).Info("Test")
resultCount := len(mem.Entries)
if resultCount != 1 {
t.Errorf("Expected %d entries, got %d", 1, resultCount)
}
}