-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzapadapter_test.go
144 lines (119 loc) · 3.2 KB
/
zapadapter_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
// (c) 2022 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)
package zapadapter_test
import (
"context"
"encoding/json"
"fmt"
"io"
"net/url"
"strings"
"testing"
"time"
"github.com/elgopher/yala/adapter/internal/adaptertest"
"github.com/elgopher/yala/adapter/zapadapter"
"github.com/elgopher/yala/logger"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"go.uber.org/zap"
)
const message = "message"
func TestAdapter_Log(t *testing.T) {
ctx := context.Background()
t.Run("should not panic when logger is nil", func(t *testing.T) {
adapter := zapadapter.Adapter{Logger: nil}
assert.NotPanics(t, func() {
adapter.Log(ctx, logger.Entry{
Level: logger.InfoLevel,
Message: message,
})
})
})
t.Run("should log caller", func(t *testing.T) {
var builder strings.Builder
adapter := newAdapter(&builder)
// when
adapter.Log(ctx, logger.Entry{
Level: logger.InfoLevel,
Message: message,
})
// then
msg := unmarshalZapMessage(t, builder.String())
const expectedPrefix = "zapadapter/zapadapter_test.go:"
assert.Truef(t, strings.HasPrefix(msg.C, expectedPrefix), "caller %s has no prefix %s", msg.C, expectedPrefix)
})
adaptertest.Run(t, adaptertest.Subject{
NewAdapter: newAdapter,
UnmarshalMessage: unmarshalMessage,
})
}
func newAdapter(writer io.Writer) logger.Adapter { // nolint
scheme := generateUniqueScheme() // Zap does not allow to override existing scheme
_ = zap.RegisterSink(scheme, func(url *url.URL) (zap.Sink, error) {
return sinkWriter{Writer: writer}, nil
})
cfg := zap.NewDevelopmentConfig()
cfg.OutputPaths = []string{scheme + "://"}
cfg.Encoding = "json"
zapLogger, err := cfg.Build()
if err != nil {
panic(err)
}
return zapadapter.Adapter{Logger: zapLogger}
}
var sequence atomic.Uint32
func generateUniqueScheme() string {
return fmt.Sprintf("s%d", sequence.Inc())
}
type sinkWriter struct {
io.Writer
}
func (w sinkWriter) Sync() error {
return nil
}
func (w sinkWriter) Close() error {
return nil
}
var levelMapping = map[string]logger.Level{
"DEBUG": logger.DebugLevel,
"INFO": logger.InfoLevel,
"WARN": logger.WarnLevel,
"ERROR": logger.ErrorLevel,
}
func unmarshalMessage(t *testing.T, line string) adaptertest.Message {
t.Helper()
out := unmarshalZapMessage(t, line)
return adaptertest.Message{
Level: levelMapping[out.L],
Message: out.M,
Error: out.Error,
StringField: out.StringField,
IntField: out.IntField,
Int64Field: out.Int64Field,
Float32Field: out.Float32Field,
Float64Field: out.Float64Field,
TimeField: out.TimeField,
InterfaceField: out.InterfaceField,
}
}
func unmarshalZapMessage(t *testing.T, line string) zapMessage {
t.Helper()
out := zapMessage{}
err := json.Unmarshal([]byte(line), &out)
require.NoError(t, err)
return out
}
type zapMessage struct {
L string // level
M string // message
C string // caller
Error string
StringField string
IntField int
Int64Field int64
Float32Field float32
Float64Field float64
TimeField time.Time
InterfaceField adaptertest.InterfaceField
}