-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
54 lines (44 loc) · 1.34 KB
/
log.go
File metadata and controls
54 lines (44 loc) · 1.34 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 supervisor
import (
"log/slog"
"net/http"
"os"
"github.com/wastedcode/supervisor/errors"
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
)
const (
KeyRequestID = "request.id"
KeyHttpRoute = string(semconv.HTTPRouteKey)
KeyHttpRequest = "http.request"
KeyHttpMethod = string(semconv.HTTPRequestMethodKey)
KeyHttpPath = "http.request.path"
KeyHttpRequestClientIP = string(semconv.ClientAddressKey)
KeyHttpResponseSize = string(semconv.HTTPResponseSizeKey)
KeyHttpRequestDuration = string(semconv.HTTPClientRequestDurationName)
KeyHttpResponseStatusCode = string(semconv.HTTPStatusCodeKey)
)
func getSimpleJsonLogger(level slog.Level) *slog.Logger {
return slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: level}))
}
type HttpRequest struct {
r *http.Request
}
func NewHttpRequest(r *http.Request) *HttpRequest {
return &HttpRequest{r: r}
}
func (r HttpRequest) LogValue() slog.Value {
return slog.GroupValue(
slog.String(KeyHttpMethod, r.r.Method),
slog.String(KeyHttpPath, r.r.URL.Path),
slog.String(KeyHttpRequestClientIP, GetIPFromHeaders(r.r).String()),
)
}
type errLog struct {
err error
}
func newErrLog(err error) errLog {
return errLog{err: err}
}
func (err errLog) LogValue() slog.Value {
return errors.LogValue(err.err)
}