forked from go-chi/httplog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
54 lines (44 loc) · 1.19 KB
/
main.go
File metadata and controls
54 lines (44 loc) · 1.19 KB
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
package main
import (
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
httpzaplog "github.com/illumitacit/httpzaplog"
"go.uber.org/zap"
)
func main() {
// Logger
opts := &httpzaplog.Options{
Logger: zap.Must(zap.NewProduction()),
Concise: true,
}
// Service
r := chi.NewRouter()
r.Use(httpzaplog.RequestLogger(opts))
r.Use(middleware.Heartbeat("/ping"))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world"))
})
r.Get("/panic", func(w http.ResponseWriter, r *http.Request) {
panic("oh no")
})
r.Get("/info", func(w http.ResponseWriter, r *http.Request) {
oplog := httpzaplog.LogEntry(r.Context())
w.Header().Add("Content-Type", "text/plain")
oplog.Info("info here")
w.Write([]byte("info here"))
})
r.Get("/warn", func(w http.ResponseWriter, r *http.Request) {
oplog := httpzaplog.LogEntry(r.Context())
oplog.Warn("warn here")
w.WriteHeader(400)
w.Write([]byte("warn here"))
})
r.Get("/err", func(w http.ResponseWriter, r *http.Request) {
oplog := httpzaplog.LogEntry(r.Context())
oplog.Error("err here")
w.WriteHeader(500)
w.Write([]byte("err here"))
})
http.ListenAndServe(":5555", r)
}