forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
111 lines (99 loc) · 3.09 KB
/
logger.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
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testutils
import (
"encoding/json"
"fmt"
"strings"
"sync"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
)
// NewLogger creates a new zap.Logger backed by a zaptest.Buffer, which is also returned.
func NewLogger() (*zap.Logger, *Buffer) {
encoder := zapcore.NewJSONEncoder(zapcore.EncoderConfig{
MessageKey: "msg",
LevelKey: "level",
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
})
buf := &Buffer{}
logger := zap.New(
zapcore.NewCore(encoder, buf, zapcore.DebugLevel),
)
return logger, buf
}
// Buffer wraps zaptest.Buffer and provides convenience method JSONLine(n)
type Buffer struct {
sync.RWMutex
zaptest.Buffer
}
// JSONLine reads n-th line from the buffer and converts it to JSON.
func (b *Buffer) JSONLine(n int) map[string]string {
data := make(map[string]string)
line := b.Lines()[n]
if err := json.Unmarshal([]byte(line), &data); err != nil {
return map[string]string{
"error": err.Error(),
}
}
return data
}
// NB. the below functions overwrite the existing functions so that logger is threadsafe.
// This is not that fragile given how if the API were to change underneath in zap, the overwritten
// function will fail to compile.
// Lines overwrites zaptest.Buffer.Lines() to make it thread safe
func (b *Buffer) Lines() []string {
b.RLock()
defer b.RUnlock()
return b.Buffer.Lines()
}
// Stripped overwrites zaptest.Buffer.Stripped() to make it thread safe
func (b *Buffer) Stripped() string {
b.RLock()
defer b.RUnlock()
return b.Buffer.Stripped()
}
// String overwrites zaptest.Buffer.String() to make it thread safe
func (b *Buffer) String() string {
b.RLock()
defer b.RUnlock()
return b.Buffer.String()
}
// Write overwrites zaptest.Buffer.bytes.Buffer.Write() to make it thread safe
func (b *Buffer) Write(p []byte) (int, error) {
b.Lock()
defer b.Unlock()
return b.Buffer.Write(p)
}
// LogMatcher is a helper func that returns true if the subStr appears more than 'occurrences' times in the logs.
var LogMatcher = func(occurrences int, subStr string, logs []string) (bool, string) {
errMsg := fmt.Sprintf("subStr '%s' does not occur %d time(s) in %v", subStr, occurrences, logs)
if len(logs) < occurrences {
return false, errMsg
}
var count int
for _, log := range logs {
if strings.Contains(log, subStr) {
count++
}
}
if count >= occurrences {
return true, ""
}
return false, errMsg
}