-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexamples_test.go
228 lines (180 loc) · 4.95 KB
/
examples_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
/*
* Copyright (c) Fabio da Silva Ribeiro <[email protected]>
* SPDX-License-Identifier: MIT
*/
package middleware_test
import (
"os"
charm "github.com/charmbracelet/log"
"github.com/labstack/echo/v4"
emw "github.com/labstack/echo/v4/middleware"
"github.com/rs/zerolog"
"github.com/sirupsen/logrus"
"go.opencensus.io/stats/view"
"go.uber.org/zap"
middleware "github.com/faabiosr/echo-middleware"
)
// This example registers the ZeroLog middleware with default configuration.
func ExampleZeroLog() {
e := echo.New()
// Middleware
e.Use(middleware.ZeroLog())
}
// This example registers the ZeroLog middleware with custom configuration.
func ExampleZeroLogWithConfig() {
e := echo.New()
// Custom zerolog logger instance
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
// Middleware
logConfig := middleware.ZeroLogConfig{
Logger: logger,
FieldMap: map[string]string{
"uri": "@uri",
"host": "@host",
"method": "@method",
"status": "@status",
},
}
e.Use(middleware.ZeroLogWithConfig(logConfig))
}
// This example register the ZeroLog log error function to echo middleware
// Recover.
func ExampleZeroLogRecoverFn() {
e := echo.New()
// Custom zerolog logger instance
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
// Middleware
e.Use(emw.RecoverWithConfig(emw.RecoverConfig{
LogErrorFunc: middleware.ZeroLogRecoverFn(logger),
}))
}
// This example registers the Logrus middleware with default configuration.
func ExampleLogrus() {
e := echo.New()
// Middleware
e.Use(middleware.Logrus())
}
// This example registers the Logrus middleware with custom configuration.
func ExampleLogrusWithConfig() {
e := echo.New()
// Custom logrus logger instance
logger := logrus.New()
// Middleware
logConfig := middleware.LogrusConfig{
Logger: logger,
FieldMap: map[string]string{
"uri": "@uri",
"host": "@host",
"method": "@method",
"status": "@status",
},
}
e.Use(middleware.LogrusWithConfig(logConfig))
}
// This example register the Logrus log error function to echo middleware
// Recover.
func ExampleLogrusRecoverFn() {
e := echo.New()
// Custom logrus logger instance
logger := logrus.New()
// Middleware
e.Use(emw.RecoverWithConfig(emw.RecoverConfig{
LogErrorFunc: middleware.LogrusRecoverFn(logger),
}))
}
// This example registers the OpenCensus middleware with default configuration.
func ExampleOpenCensus() {
e := echo.New()
// Middleware
e.Use(middleware.OpenCensus())
}
// This example registers the OpenCensus middleware with custom configuration.
func ExampleOpenCensusWithConfig() {
e := echo.New()
// Middleware
cfg := middleware.OpenCensusConfig{
Views: []*view.View{
middleware.OpenCensusRequestCount,
},
}
e.Use(middleware.OpenCensusWithConfig(cfg))
}
// This example registers the ZapLog middleware with default configuration.
func ExampleZapLog() {
e := echo.New()
// Middleware
e.Use(middleware.ZapLog())
}
// This example registers the ZapLog middleware with custom configuration.
func ExampleZapLogWithConfig() {
e := echo.New()
// Custom ZapLog logger instance
logger, _ := zap.NewProduction()
// Middleware
logConfig := middleware.ZapLogConfig{
Logger: logger,
FieldMap: map[string]string{
"uri": "@uri",
"host": "@host",
"method": "@method",
"status": "@status",
},
}
e.Use(middleware.ZapLogWithConfig(logConfig))
}
// This example register the ZapLog log error function to echo middleware
// Recover.
func ExampleZapLogRecoverFn() {
e := echo.New()
// Custom ZapLog logger instance
logger, _ := zap.NewProduction()
// Middleware
e.Use(emw.RecoverWithConfig(emw.RecoverConfig{
LogErrorFunc: middleware.ZapLogRecoverFn(logger),
}))
}
// This example registers the CharmBracelet Log middleware with default configuration.
func ExampleCharmLog() {
e := echo.New()
// Middleware
e.Use(middleware.CharmLog())
}
// This example registers the CharmBracelet Log middleware with custom configuration.
func ExampleCharmLogWithConfig() {
e := echo.New()
// Middleware
logConfig := middleware.CharmLogConfig{
Logger: charm.Default(),
FieldMap: map[string]string{
"uri": "@uri",
"host": "@host",
"method": "@method",
"status": "@status",
},
}
e.Use(middleware.CharmLogWithConfig(logConfig))
}
// This example register the CharmLog log error function to echo middleware
// Recover.
func ExampleCharmLogRecoverFn() {
e := echo.New()
// Middleware
e.Use(emw.RecoverWithConfig(emw.RecoverConfig{
LogErrorFunc: middleware.CharmLogRecoverFn(charm.Default()),
}))
}
// This example registers the RequestID middleware with default configuration.
func ExampleRequestID() {
e := echo.New()
// Middleware
e.Use(middleware.RequestID())
}
// This example registers the RequestID middleware with custom configuration.
func ExampleRequestIDWithConfig() {
e := echo.New()
// Middleware
config := middleware.RequestIDConfig{
TargetHeader: echo.HeaderXRequestID,
}
e.Use(middleware.RequestIDWithConfig(config))
}