-
Notifications
You must be signed in to change notification settings - Fork 42
/
stdlog_test.go
277 lines (234 loc) · 6.04 KB
/
stdlog_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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MIT
package hclog
import (
"bytes"
"fmt"
"log"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStdlogAdapter_PickLevel(t *testing.T) {
t.Run("picks debug level", func(t *testing.T) {
var s stdlogAdapter
level, rest := s.pickLevel("[DEBUG] coffee?")
assert.Equal(t, Debug, level)
assert.Equal(t, "coffee?", rest)
})
t.Run("picks trace level", func(t *testing.T) {
var s stdlogAdapter
level, rest := s.pickLevel("[TRACE] coffee?")
assert.Equal(t, Trace, level)
assert.Equal(t, "coffee?", rest)
})
t.Run("picks info level", func(t *testing.T) {
var s stdlogAdapter
level, rest := s.pickLevel("[INFO] coffee?")
assert.Equal(t, Info, level)
assert.Equal(t, "coffee?", rest)
})
t.Run("picks warn level", func(t *testing.T) {
var s stdlogAdapter
level, rest := s.pickLevel("[WARN] coffee?")
assert.Equal(t, Warn, level)
assert.Equal(t, "coffee?", rest)
})
t.Run("picks error level", func(t *testing.T) {
var s stdlogAdapter
level, rest := s.pickLevel("[ERROR] coffee?")
assert.Equal(t, Error, level)
assert.Equal(t, "coffee?", rest)
})
t.Run("picks error as err level", func(t *testing.T) {
var s stdlogAdapter
level, rest := s.pickLevel("[ERR] coffee?")
assert.Equal(t, Error, level)
assert.Equal(t, "coffee?", rest)
})
}
func TestStdlogAdapter_TrimTimestamp(t *testing.T) {
cases := []struct {
name string
input string
expect string
}{
{
name: "Go log Ldate",
input: "2009/01/23 [ERR] message",
expect: "[ERR] message",
},
{
name: "Go log Ldate|Ltime",
input: "2009/01/23 01:23:23 [ERR] message",
expect: "[ERR] message",
},
{
name: "Go log Ldate|Ltime|Lmicroseconds",
input: "2009/01/23 01:23:23.123123 [ERR] message",
expect: "[ERR] message",
},
{
name: "Go log Ltime",
input: "01:23:23 [ERR] message",
expect: "[ERR] message",
},
{
name: "Go log Ltime|Lmicroseconds",
input: "01:23:23.123123 [ERR] message",
expect: "[ERR] message",
},
{
name: "ISO 8601 date",
input: "2021-10-28 [ERR] message",
expect: "[ERR] message",
},
{
name: "ISO 8601 date and time",
input: "2021-10-28T19:27:28+00:00 [ERR] message",
expect: "[ERR] message",
},
{
name: "ISO 8601 date and time zulu",
input: "2021-10-28T19:27:28Z [ERR] message",
expect: "[ERR] message",
},
{
name: "no timestamp",
input: "[ERR] message",
expect: "[ERR] message",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var s stdlogAdapter
got := s.trimTimestamp(c.input)
assert.Equal(t, c.expect, got)
})
}
}
func TestStdlogAdapter_ForceLevel(t *testing.T) {
cases := []struct {
name string
forceLevel Level
inferLevels bool
write string
expect string
}{
{
name: "force error",
forceLevel: Error,
write: "this is a test",
expect: "[ERROR] test: this is a test\n",
},
{
name: "force error overrides infer",
forceLevel: Error,
inferLevels: true,
write: "[DEBUG] this is a test",
expect: "[ERROR] test: this is a test\n",
},
{
name: "force error and strip debug",
forceLevel: Error,
write: "[DEBUG] this is a test",
expect: "[ERROR] test: this is a test\n",
},
{
name: "force trace",
forceLevel: Trace,
write: "this is a test",
expect: "[TRACE] test: this is a test\n",
},
{
name: "force trace and strip higher level error",
forceLevel: Trace,
write: "[WARN] this is a test",
expect: "[TRACE] test: this is a test\n",
},
{
name: "force with invalid level",
forceLevel: -10,
write: "this is a test",
expect: "[INFO] test: this is a test\n",
},
{
name: "infer debug",
forceLevel: NoLevel,
inferLevels: true,
write: "[DEBUG] debug info",
expect: "[DEBUG] test: debug info\n",
},
{
name: "info is used if not forced and cannot infer",
forceLevel: NoLevel,
inferLevels: false,
write: "some message",
expect: "[INFO] test: some message\n",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var stderr bytes.Buffer
logger := New(&LoggerOptions{
Name: "test",
Output: &stderr,
Level: Trace,
})
s := &stdlogAdapter{
log: logger,
forceLevel: c.forceLevel,
inferLevels: c.inferLevels,
}
_, err := s.Write([]byte(c.write))
assert.NoError(t, err)
errStr := stderr.String()
errDataIdx := strings.IndexByte(errStr, ' ')
errRest := errStr[errDataIdx+1:]
assert.Equal(t, c.expect, errRest)
})
}
}
func TestFromStandardLogger(t *testing.T) {
var buf bytes.Buffer
sl := log.New(&buf, "test-stdlib-log ", log.Ltime)
hl := FromStandardLogger(sl, &LoggerOptions{
Name: "hclog-inner",
IncludeLocation: true,
})
hl.Info("this is a test", "name", "tester", "count", 1)
_, file, line, ok := runtime.Caller(0)
require.True(t, ok)
actual := buf.String()
suffix := fmt.Sprintf(
"[INFO] go-hclog/%s:%d: hclog-inner: this is a test: name=tester count=1\n",
filepath.Base(file), line-1)
require.Equal(t, suffix, actual[25:])
prefix := "test-stdlib-log "
require.Equal(t, prefix, actual[:16])
}
func TestFromStandardLogger_helper(t *testing.T) {
var buf bytes.Buffer
sl := log.New(&buf, "test-stdlib-log ", log.Ltime)
hl := FromStandardLogger(sl, &LoggerOptions{
Name: "hclog-inner",
IncludeLocation: true,
AdditionalLocationOffset: 1,
})
helper := func() {
hl.Info("this is a test", "name", "tester", "count", 1)
}
helper()
_, file, line, ok := runtime.Caller(0)
require.True(t, ok)
actual := buf.String()
suffix := fmt.Sprintf(
"[INFO] go-hclog/%s:%d: hclog-inner: this is a test: name=tester count=1\n",
filepath.Base(file), line-1)
require.Equal(t, suffix, actual[25:])
prefix := "test-stdlib-log "
require.Equal(t, prefix, actual[:16])
}