-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger_test.go
147 lines (133 loc) · 3.47 KB
/
logger_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
138
139
140
141
142
143
144
145
146
147
// Copyright 2013 Marc Weistroff. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package log
import (
"bytes"
"testing"
"time"
)
func ExampleLogger() {
// Will outputs to Stdout all messages where severity is >= WARNING
handler := NewStdoutHandler(WARNING)
// Use the LINE_FORMAT_MINIMAL format
handler.SetFormatter(NewMinimalLineFormatter())
// Instantiates a new logger with "example" as name
logger := NewLogger("example")
logger.PushHandler(handler)
// Start logging
logger.Debug("Debug message that won't be displayed")
logger.Warning("I sense a disturbance in the force")
logger.Error("This is an error")
// Output:
// example.WARNING: I sense a disturbance in the force
// example.ERROR: This is an error
}
func TestLogger(t *testing.T) {
buffer := new(bytes.Buffer)
handler := NewBufferHandler(buffer, NOTICE)
handler.SetFormatter(&LineFormatter{LineFormat: "%channel%.%level_name%: %message%"})
logger := NewLogger("test")
logger.PushHandler(handler)
logger.Debug("foobar")
if "" != buffer.String() {
t.Error(buffer.String())
}
buffer.Reset()
logger.Info("foobar")
if "" != buffer.String() {
t.Error(buffer.String())
}
buffer.Reset()
logger.Notice("foobar")
if "test.NOTICE: foobar" != buffer.String() {
t.Error(buffer.String())
}
buffer.Reset()
logger.Warning("foobar")
if "test.WARNING: foobar" != buffer.String() {
t.Error(buffer.String())
}
buffer.Reset()
logger.Error("foobar")
if "test.ERROR: foobar" != buffer.String() {
t.Error(buffer.String())
}
buffer.Reset()
logger.Critical("foobar")
if "test.CRITICAL: foobar" != buffer.String() {
t.Error(buffer.String())
}
buffer.Reset()
logger.Alert("foobar")
if "test.ALERT: foobar" != buffer.String() {
t.Error(buffer.String())
}
buffer.Reset()
logger.Emergency("foobar")
if "test.EMERGENCY: foobar" != buffer.String() {
t.Error(buffer.String())
}
}
func TestLoggerPushPopProcessors(t *testing.T) {
p1 := NewProcessor(func(r *Record) {}) // Will be called last
p2 := NewProcessor(func(r *Record) {})
l := NewLogger("test")
l.PushProcessor(p1)
l.PushProcessor(p2)
if len(l.processors) != 2 {
t.Error("processor count mismatch")
}
if l.processors[0] != p2 {
t.Error("processor mismatch")
}
if l.processors[1] != p1 {
t.Error("processor mismatch")
}
l.PopProcessor()
if len(l.processors) != 1 {
t.Error("processor count mismatch")
}
if l.processors[0] != p1 {
t.Error("processor mismatch")
}
}
func TestLoggerPushPopHandler(t *testing.T) {
buffer := new(bytes.Buffer)
h1 := NewBufferHandler(buffer, DEBUG)
h2 := NewBufferHandler(buffer, DEBUG)
l := &Logger{}
l.PushHandler(h1)
l.PushHandler(h2)
if len(l.handlers) != 2 {
t.Error("processor count mismatch")
}
if l.handlers[0] != h2 {
t.Error("processor mismatch")
}
if l.handlers[1] != h1 {
t.Error("processor mismatch")
}
l.PopHandler()
if len(l.handlers) != 1 {
t.Error("processor count mismatch")
}
if l.handlers[0] != h1 {
t.Error("processor mismatch")
}
}
func TestLoggerWithDefaultProcessor(t *testing.T) {
buffer := new(bytes.Buffer)
h1 := NewBufferHandler(buffer, DEBUG)
h1.SetFormatter(&LineFormatter{LineFormat: "%message% %extra%"})
l := NewLogger("channel")
l.PushHandler(h1)
p := NewProcessor(func(r *Record) {
r.Extra["go.date"] = time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC)
})
l.PushProcessor(p)
l.Debug("foobar")
if "foobar go.date=2009-11-10T23:00:00Z" != buffer.String() {
t.Error(buffer.String())
}
}