-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (124 loc) · 4.64 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
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
package main
import (
"errors"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/traceid"
"github.com/golang-cz/devslog"
"github.com/golang-cz/httplog"
)
func main() {
// JSON logger for production.
// For localhost development, you can replace it with slog.NewTextHandler()
// or with a pretty logger like github.com/golang-cz/devslog.
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)).With(
slog.String("app", "example-app"),
slog.String("version", "v1.0.0-a1fa420"),
slog.String("env", "production"),
)
isLocalhost := os.Getenv("ENV") == "localhost"
if isLocalhost {
// Pretty logger for localhost development.
logger = slog.New(devslog.NewHandler(os.Stdout, &devslog.Options{
SortKeys: true,
MaxErrorStackTrace: 5,
MaxSlicePrintSize: 20,
}))
}
// Set as a default logger for both slog and log.
slog.SetDefault(logger)
slog.SetLogLoggerLevel(slog.LevelError)
// Service
r := chi.NewRouter()
r.Use(middleware.Heartbeat("/ping"))
// Propagate or create new TraceId header.
r.Use(traceid.Middleware)
// Request logger
r.Use(httplog.RequestLogger(logger, &httplog.Options{
// Level defines the verbosity of the request logs:
// slog.LevelDebug - log both request starts & responses (incl. OPTIONS)
// slog.LevelInfo - log all responses (excl. OPTIONS)
// slog.LevelWarn - log 4xx and 5xx responses only (except for 429)
// slog.LevelError - log 5xx responses only
Level: slog.LevelInfo,
// Concise mode causes fewer log attributes to be printed in request logs.
// This is useful if your console is too noisy during development.
Concise: isLocalhost,
// RecoverPanics recovers from panics occurring in the underlying HTTP handlers
// and middlewares. It returns HTTP 500 unless response status was already set.
//
// NOTE: Panics are logged as errors automatically, regardless of this setting.
RecoverPanics: true,
// Select request/response headers to be logged explicitly.
LogRequestHeaders: []string{"User-Agent", "Origin", "Referer", traceid.Header},
LogResponseHeaders: []string{traceid.Header},
// You can log request/request body. Useful for debugging.
LogRequestBody: false,
LogResponseBody: false,
}))
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Set request log attribute from within middleware.
httplog.SetAttrs(ctx, slog.String("user", "user1"))
next.ServeHTTP(w, r.WithContext(ctx))
})
})
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world \n"))
})
r.Get("/panic", func(w http.ResponseWriter, r *http.Request) {
panic("oh no")
})
r.Get("/info", func(w http.ResponseWriter, r *http.Request) {
httplog.InfoContext(r.Context(), "info here")
w.Header().Add("Content-Type", "text/plain")
w.Write([]byte("info here \n"))
})
r.Get("/warn", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
httplog.WarnContext(ctx, "warn here")
w.WriteHeader(400)
w.Write([]byte("warn here \n"))
})
r.Get("/err", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Log error explicitly.
err := errors.New("err here")
httplog.ErrorContext(ctx, "msg here", slog.Any("error", err))
// Logging with the global logger also works.
slog.Default().With(slog.Group("ImpGroup", slog.String("account", "id"))).ErrorContext(ctx, "doesn't exist")
slog.Default().ErrorContext(ctx, "oops, error occurred")
w.WriteHeader(500)
w.Write([]byte("oops, err \n"))
})
r.Post("/body", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Log request/response bodies for Admin requests.
if r.Header.Get("Authorization") == "Bearer ADMIN-SECRET" {
httplog.LogRequestBody(ctx)
httplog.LogResponseBody(ctx)
}
w.Write([]byte(`{"response": "payload"}`))
})
fmt.Println("Enable pretty logs with:")
fmt.Println(" ENV=localhost go run ./")
fmt.Println()
fmt.Println("Try these commands from a new terminal window:")
fmt.Println(" curl -v http://localhost:8000")
fmt.Println(" curl -v http://localhost:8000/panic")
fmt.Println(" curl -v http://localhost:8000/info")
fmt.Println(" curl -v http://localhost:8000/warn")
fmt.Println(" curl -v http://localhost:8000/err")
fmt.Println(` curl -v http://localhost:8000/body -X POST --data '{"request": "data"}'`)
fmt.Println(` curl -v http://localhost:8000/body -X POST --data '{"request": "data"}' -H "Authorization: Bearer ADMIN-SECRET"`)
fmt.Println()
if err := http.ListenAndServe("localhost:8000", r); err != http.ErrAbortHandler {
log.Fatal(err)
}
}