-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc_test.go
51 lines (41 loc) · 1.54 KB
/
doc_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
package zaphttp_test
import (
"errors"
"net/http"
"time"
"github.com/marnixbouhuis/zaphttp"
"go.uber.org/zap"
)
func Example() {
logger := zap.NewExample()
zap.ReplaceGlobals(logger)
defer func() {
_ = logger.Sync()
}()
mux := http.NewServeMux()
mux.HandleFunc("/demo/{$}", func(w http.ResponseWriter, req *http.Request) {
// Optional, get the logger for this request from the context.
// If you are using opentelemetry, the trace ID is automatically injected into each log message.
l := zaphttp.FromContext(req.Context())
// Optional, log something with the request logger.
l.Info("Some message!")
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("Hello world!"))
})
requestLogger := zaphttp.NewHandler(
zaphttp.WithLogger(logger), // If no logger is supplied, zap.L(), will be used.
zaphttp.WithTraceFormatter(zaphttp.ElasticCommonSchemaFormatter), // If no format for trace metadata is supplied, ECS is used.
zaphttp.WithRequestFormatter(zaphttp.ElasticCommonSchemaFormatter), // If no format for request metadata is supplied, ECS is used.
)
s := &http.Server{
Addr: ":8080",
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
Handler: requestLogger(mux), // Wrap the mux, all requests will now be logged.
}
// Do graceful shutdown of HTTP server here...
if err := s.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Error("Failed to start server", zap.Error(err))
}
}