-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetup_test.go
More file actions
134 lines (110 loc) · 3.88 KB
/
setup_test.go
File metadata and controls
134 lines (110 loc) · 3.88 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
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
package logf
import (
"bytes"
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewLoggerDefaults(t *testing.T) {
// Build with no options — should use JSON encoder, LevelDebug, os.Stderr.
// We redirect output to verify it produces something.
var buf bytes.Buffer
logger := NewLogger().Output(&buf).Build()
require.NotNil(t, logger)
logger.Info(context.Background(), "hello")
assert.Contains(t, buf.String(), `"msg":"hello"`)
}
func TestNewLoggerLevel(t *testing.T) {
var buf bytes.Buffer
logger := NewLogger().Level(LevelError).Output(&buf).Build()
logger.Debug(context.Background(), "debug-msg")
logger.Info(context.Background(), "info-msg")
logger.Warn(context.Background(), "warn-msg")
assert.Empty(t, buf.String(), "debug/info/warn should be filtered at LevelError")
logger.Error(context.Background(), "error-msg")
assert.Contains(t, buf.String(), "error-msg")
}
func TestNewLoggerOutput(t *testing.T) {
var buf bytes.Buffer
logger := NewLogger().Output(&buf).Build()
logger.Error(context.Background(), "to-buffer")
assert.Contains(t, buf.String(), "to-buffer")
}
func TestNewLoggerEncoder(t *testing.T) {
var buf bytes.Buffer
enc := NewTextEncoder(TextEncoderConfig{NoColor: true, DisableFieldTime: true})
logger := NewLogger().Encoder(enc).Output(&buf).Build()
logger.Error(context.Background(), "text-output")
out := buf.String()
// Text encoder output contains level in brackets, not JSON.
assert.Contains(t, out, "[ERR]")
assert.Contains(t, out, "text-output")
}
func TestNewLoggerEncoderFrom(t *testing.T) {
var buf bytes.Buffer
logger := NewLogger().
EncoderFrom(JSON().TimeKey("time").DisableLevel()).
Output(&buf).
Build()
logger.Error(context.Background(), "enc-from")
out := buf.String()
assert.Contains(t, out, `"time":`)
assert.NotContains(t, out, `"level":`)
assert.Contains(t, out, `"msg":"enc-from"`)
}
func TestNewLoggerContext(t *testing.T) {
var buf bytes.Buffer
logger := NewLogger().Output(&buf).Context().Build()
ctx := With(context.Background(), String("req_id", "abc123"))
logger.Info(ctx, "with-context")
assert.Contains(t, buf.String(), "abc123")
}
func TestNewLoggerContextWithFieldSource(t *testing.T) {
var buf bytes.Buffer
src := func(ctx context.Context) []Field {
return []Field{String("injected", "yes")}
}
logger := NewLogger().Output(&buf).Context(src).Build()
logger.Info(context.Background(), "with-source")
assert.Contains(t, buf.String(), "injected")
assert.Contains(t, buf.String(), "yes")
}
func TestNewLoggerCombined(t *testing.T) {
var buf bytes.Buffer
logger := NewLogger().
Level(LevelInfo).
EncoderFrom(JSON().MsgKey("message")).
Output(&buf).
Context().
Build()
logger.Debug(context.Background(), "should-not-appear")
assert.Empty(t, buf.String())
logger.Info(context.Background(), "should-appear")
assert.Contains(t, buf.String(), `"message":"should-appear"`)
}
func TestNewLoggerEncoderClearsEncoderFrom(t *testing.T) {
// Setting Encoder after EncoderFrom should use the direct encoder.
var buf bytes.Buffer
enc := NewTextEncoder(TextEncoderConfig{NoColor: true, DisableFieldTime: true})
logger := NewLogger().
EncoderFrom(JSON()). // set builder first
Encoder(enc). // then override with direct encoder
Output(&buf).
Build()
logger.Error(context.Background(), "direct-enc")
assert.Contains(t, buf.String(), "[ERR]")
}
func TestNewLoggerEncoderFromClearsEncoder(t *testing.T) {
// Setting EncoderFrom after Encoder should use the builder.
var buf bytes.Buffer
enc := NewTextEncoder(TextEncoderConfig{NoColor: true, DisableFieldTime: true})
logger := NewLogger().
Encoder(enc). // set direct encoder first
EncoderFrom(JSON()). // then override with builder
Output(&buf).
Build()
logger.Error(context.Background(), "builder-enc")
// Should be JSON, not text.
assert.Contains(t, buf.String(), `"msg":"builder-enc"`)
}