-
Notifications
You must be signed in to change notification settings - Fork 7
/
log.go
39 lines (34 loc) · 812 Bytes
/
log.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
package main
import (
"context"
"log/slog"
"net/http"
"os"
"strconv"
"go.yhsif.com/ctxslog"
)
func initLogger() {
logger := ctxslog.New(
ctxslog.WithAddSource(true),
ctxslog.WithLevel(slog.LevelDebug),
ctxslog.WithCallstack(slog.LevelError),
ctxslog.WithReplaceAttr(ctxslog.ChainReplaceAttr(
ctxslog.GCPKeys,
ctxslog.StringDuration,
ctxslog.StringInt,
)),
)
if v, ok := os.LookupEnv("VERSION_TAG"); ok {
logger = logger.With(slog.String("v", v))
}
slog.SetDefault(logger)
}
func logContext(r *http.Request) context.Context {
return ctxslog.Attach(
r.Context(),
"httpRequest", ctxslog.HTTPRequest(r, ctxslog.GCPRealIP),
)
}
func chatContext(ctx context.Context, id int64) context.Context {
return ctxslog.Attach(ctx, slog.String("chatID", strconv.FormatInt(id, 10)))
}