forked from uber-go/zap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
global_test.go
139 lines (120 loc) · 4.12 KB
/
global_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
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package zap
import (
"log"
"sync"
"testing"
"time"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
"go.uber.org/zap/zaptest/observer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
)
func TestReplaceGlobals(t *testing.T) {
initialL := *L()
initialS := *S()
withLogger(t, DebugLevel, nil, func(l *Logger, logs *observer.ObservedLogs) {
L().Info("no-op")
S().Info("no-op")
assert.Equal(t, 0, logs.Len(), "Expected initial logs to go to default no-op global.")
defer ReplaceGlobals(l)()
L().Info("captured")
S().Info("captured")
expected := observer.LoggedEntry{
Entry: zapcore.Entry{Message: "captured"},
Context: []zapcore.Field{},
}
assert.Equal(
t,
[]observer.LoggedEntry{expected, expected},
logs.AllUntimed(),
"Unexpected global log output.",
)
})
assert.Equal(t, initialL, *L(), "Expected func returned from ReplaceGlobals to restore initial L.")
assert.Equal(t, initialS, *S(), "Expected func returned from ReplaceGlobals to restore initial S.")
}
func TestGlobalsConcurrentUse(t *testing.T) {
var (
stop atomic.Bool
wg sync.WaitGroup
)
for i := 0; i < 100; i++ {
wg.Add(2)
go func() {
for !stop.Load() {
ReplaceGlobals(NewNop())
}
wg.Done()
}()
go func() {
for !stop.Load() {
L().With(Int("foo", 42)).Named("main").WithOptions(Development()).Info("")
S().Info("")
}
wg.Done()
}()
}
zaptest.Sleep(100 * time.Millisecond)
stop.Toggle()
wg.Wait()
}
func TestNewStdLog(t *testing.T) {
withLogger(t, DebugLevel, []Option{AddCaller()}, func(l *Logger, logs *observer.ObservedLogs) {
std := NewStdLog(l)
std.Print("redirected")
require.Equal(t, 1, logs.Len(), "Expected exactly one entry to be logged.")
entry := logs.AllUntimed()[0]
assert.Equal(t, []zapcore.Field{}, entry.Context, "Unexpected entry context.")
assert.Equal(t, "redirected", entry.Entry.Message, "Unexpected entry message.")
assert.Regexp(
t,
`go.uber.org/zap/global_test.go:\d+$`,
entry.Entry.Caller.String(),
"Unexpected caller annotation.",
)
})
}
func TestRedirectStdLog(t *testing.T) {
initialFlags := log.Flags()
initialPrefix := log.Prefix()
withLogger(t, DebugLevel, nil, func(l *Logger, logs *observer.ObservedLogs) {
defer RedirectStdLog(l)()
log.Print("redirected")
assert.Equal(t, []observer.LoggedEntry{{
Entry: zapcore.Entry{Message: "redirected"},
Context: []zapcore.Field{},
}}, logs.AllUntimed(), "Unexpected global log output.")
})
assert.Equal(t, initialFlags, log.Flags(), "Expected to reset initial flags.")
assert.Equal(t, initialPrefix, log.Prefix(), "Expected to reset initial prefix.")
}
func TestRedirectStdLogCaller(t *testing.T) {
withLogger(t, DebugLevel, []Option{AddCaller()}, func(l *Logger, logs *observer.ObservedLogs) {
defer RedirectStdLog(l)()
log.Print("redirected")
entries := logs.All()
require.Len(t, entries, 1, "Unexpected number of logs.")
assert.Contains(t, entries[0].Entry.Caller.File, "global_test.go", "Unexpected caller annotation.")
})
}