forked from glaslos/loggers-mapper-revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrevel_test.go
223 lines (188 loc) · 5.32 KB
/
revel_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package revel
import (
"bytes"
"fmt"
"io"
"path/filepath"
"regexp"
"runtime"
"strings"
"testing"
"github.com/Vivino/go-loggers"
"github.com/Vivino/go-loggers/log"
log15 "github.com/inconshreveable/log15"
"github.com/revel/revel"
rlogger "github.com/revel/revel/logger"
"github.com/stretchr/testify/assert"
)
func TestRevelInterface(t *testing.T) {
var _ loggers.Contextual = NewLogger()
}
func TestRevelLevelOutputWithColor(t *testing.T) {
l, b := newBufferedRevelLog()
l.Debugln("\x1b[30mThis text will have black color\x1b[0m")
l.Debugln("This text will have default color")
var expectedMatch = []string{
"TRACE.*This text will have black color.+$",
"TRACE.*This text will have default color",
}
actual := b.String()
lines := strings.Split(actual, "\n")
k := 1 // Offset for lines before expected
for i, expected := range expectedMatch {
if ok, _ := regexp.Match(expected, []byte(lines[i+k])); !ok {
t.Errorf("Log output mismatch: `%s` (actual) != `%s` (expected)", lines[i+k], expected)
}
}
}
func TestRevelLevelOutput(t *testing.T) {
l, b := newBufferedRevelLog()
l.Info("This is a test")
expectedMatch := "INFO.*This is a test\n"
actual := b.String()
if ok, _ := regexp.Match(expectedMatch, []byte(actual)); !ok {
t.Errorf("Log output mismatch: %s (actual) != %s (expected)", actual, expectedMatch)
}
}
func TestRevelLevelfOutput(t *testing.T) {
l, b := newBufferedRevelLog()
l.Errorf("This is %s test", "a")
expectedMatch := "ERROR.*This is a test\n"
actual := b.String()
if ok, _ := regexp.Match(expectedMatch, []byte(actual)); !ok {
t.Errorf("Log output mismatch: %s (actual) != %s (expected)", actual, expectedMatch)
}
}
func TestRevelLevellnOutput(t *testing.T) {
l, b := newBufferedRevelLog()
l.Debugln("This is a test.", "So is this.")
expectedMatch := "DBUG.*This is a test. So is this.\n"
actual := b.String()
assert.Equal(t, expectedMatch, actual)
}
func TestRevelWithFieldsOutput(t *testing.T) {
l, b := newBufferedRevelLog()
l.WithFields("test", true).Warn("This is a message.")
expectedMatch := "WARN.*This is a message. test=true\n"
actual := b.String()
assert.Equal(t, expectedMatch, actual)
}
func TestRevelWithFieldsfOutput(t *testing.T) {
l, b := newBufferedRevelLog()
l.WithFields("test", true, "Error", "serious").Errorf("This is a %s.", "message")
expectedMatch := "EROR.*This is a message. test=true Error=serious\n"
actual := b.String()
assert.Equal(t, expectedMatch, actual)
}
var lvlMap = map[int]string{
0: "critical",
1: "error",
2: "warn",
3: "info",
4: "debug",
}
func logfmt(buf *bytes.Buffer, ctx []interface{}, color int) {
for i := 0; i < len(ctx); i += 2 {
if i != 0 {
buf.WriteByte(' ')
}
k := ctx[i].(string)
v := ctx[i+1].(string)
// XXX: we should probably check that all of your key bytes aren't invalid
if color > 0 {
fmt.Fprintf(buf, "\x1b[%dm%s\x1b[0m=%s", color, k, v)
} else {
buf.WriteString(k)
buf.WriteByte('=')
buf.WriteString(v)
}
}
buf.WriteByte('\n')
}
func FormatTest() log15.Format {
return log15.FormatFunc(func(r *log15.Record) []byte {
var color = 0
switch r.Lvl {
case log15.LvlCrit:
color = 35
case log15.LvlError:
color = 31
case log15.LvlWarn:
color = 33
case log15.LvlInfo:
color = 32
case log15.LvlDebug:
color = 36
}
b := &bytes.Buffer{}
lvl := strings.ToUpper(r.Lvl.String())
if color > 0 {
//fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m %s ", color, lvl, r.Msg)
fmt.Fprintf(b, "%s.*%s", lvl, r.Msg)
} else {
fmt.Fprintf(b, "[%s] %s", lvl, r.Msg)
}
// print the keys logfmt style
logfmt(b, r.Ctx, color)
return b.Bytes()
})
}
type LogHandler struct {
h log15.Handler
}
func newLogHandler(w io.Writer) *LogHandler {
return &LogHandler{
h: log15.StreamHandler(w, FormatTest()),
}
}
func (h *LogHandler) Log(r *rlogger.Record) error {
lvl, err := log15.LvlFromString(lvlMap[int(r.Level)])
if err != nil {
panic(err)
}
return h.h.Log(&log15.Record{
Msg: r.Message,
Lvl: lvl,
})
}
func newBufferedRevelLog() (loggers.Contextual, *bytes.Buffer) {
var b []byte
var bb = bytes.NewBuffer(b)
h := newLogHandler(bb)
revel.RootLog = rlogger.New()
revel.AppLog.SetHandler(rlogger.FuncHandler(h.Log))
return NewLogger(), bb
}
func TestBackTrace(t *testing.T) {
l, b := newBufferedRevelLog()
log.Logger = l
log.Error("an error")
_, file, line, _ := runtime.Caller(0)
mustContain := fmt.Sprintf("%s:%d", filepath.Base(file), line-1)
actual := b.String()
if ok := strings.Contains(actual, mustContain); !ok {
t.Errorf("Log output mismatch: %s (actual) != %s (expected)", actual, mustContain)
}
}
func TestBackTraceF(t *testing.T) {
l, b := newBufferedRevelLog()
log.Logger = l
log.Errorf("an error: %s", "value")
_, file, line, _ := runtime.Caller(0)
mustContain := fmt.Sprintf("%s:%d", filepath.Base(file), line-1)
actual := b.String()
if ok := strings.Contains(actual, mustContain); !ok {
t.Errorf("Log output mismatch: %s (actual) != %s (expected)", actual, mustContain)
}
}
func TestBackTraceLn(t *testing.T) {
l, b := newBufferedRevelLog()
log.Logger = l
log.Errorln("an error")
_, file, line, _ := runtime.Caller(0)
mustContain := fmt.Sprintf("%s:%d", filepath.Base(file), line-1)
actual := b.String()
if ok := strings.Contains(actual, mustContain); !ok {
t.Errorf("Log output mismatch: %s (actual) != %s (expected)", actual, mustContain)
}
}