-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (49 loc) · 1.34 KB
/
main.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
package main
import (
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
httplog "github.com/stn1slv/chi-httplog"
)
func main() {
// Logger
logger := httplog.NewLogger("httplog-example", httplog.Options{
// JSON: true,
// Concise: true,
Body: true,
// Tags: map[string]string{
// "version": "v1.0-81aa4244d9fc8076a",
// "env": "dev",
// },
})
// Service
r := chi.NewRouter()
r.Use(httplog.RequestLogger(logger))
r.Use(middleware.Heartbeat("/ping"))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
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 := httplog.LogEntry(r.Context())
w.Header().Add("Content-Type", "text/plain")
oplog.Info().Msg("info here")
w.Write([]byte("info here"))
})
r.Get("/warn", func(w http.ResponseWriter, r *http.Request) {
oplog := httplog.LogEntry(r.Context())
oplog.Warn().Msg("warn here")
w.WriteHeader(400)
w.Write([]byte("warn here"))
})
r.Get("/err", func(w http.ResponseWriter, r *http.Request) {
oplog := httplog.LogEntry(r.Context())
oplog.Error().Msg("err here")
w.WriteHeader(500)
w.Write([]byte("err here"))
})
http.ListenAndServe(":5555", r)
}