-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_middleware_test.go
47 lines (38 loc) · 1.24 KB
/
example_middleware_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
package logger_test
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"github.com/secureworks/logger/log"
"github.com/secureworks/logger/middleware"
_ "github.com/secureworks/logger/zerolog"
)
// You can easily integrate the Logger itself into HTTP server
// middleware too. See the middleware package for documentation
// examples.
func Example_usingMiddleware() {
config := log.DefaultConfig(nil)
config.Output = os.Stdout
logger, _ := log.Open("zerolog", config)
// Pick attributes to log. You can also skip defaults.
attrs := &middleware.HTTPRequestLogAttributes{
Headers: []string{"X-Request-Id"},
SkipDuration: true,
SkipRemoteAddr: true,
}
// Inject the logger and attributes into the middleware.
mwareFn := middleware.NewHTTPRequestMiddleware(logger, log.INFO, attrs)
handler := mwareFn(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
rw.WriteHeader(http.StatusOK)
}))
srv := httptest.NewServer(handler)
defer srv.Close()
fmt.Println()
// Send a request.
req, _ := http.NewRequest(http.MethodGet, srv.URL+"/test/path", nil)
req.Header.Add("X-Request-Id", "12345")
_, _ = srv.Client().Do(req)
// Output:
// {"http_method":"GET","http_path":"/test/path","x-request-id":"12345","level":"info"}
}