-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
69 lines (56 loc) · 2.11 KB
/
logger.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
// Copyright 2024 Bill Nixon. All rights reserved.
// Use of this source code is governed by the license found in the LICENSE file.
package webhandler
import (
"context"
"log/slog"
"net/http"
"github.com/bnixon67/webapp/util"
"github.com/bnixon67/webapp/webutil"
)
// NewRequestLogger creates and configures a logger specifically for logging
// HTTP request details, such as the method, URL, and client IP. It optionally
// includes a request ID if present.
func NewRequestLogger(r *http.Request) *slog.Logger {
attributes := []any{
slog.String("method", r.Method),
slog.String("url", r.URL.String()),
slog.String("ip", webutil.ClientIP(r)),
}
// Append request ID to the log attributes if available.
if id := RequestID(r.Context()); id != "" {
attributes = append(attributes, slog.String("id", id))
}
return slog.With(slog.Group("request", attributes...))
}
// NewRequestLoggerWithFuncName augments a request logger by adding the
// caller function's name to the log attributes.
func NewRequestLoggerWithFuncName(r *http.Request) *slog.Logger {
return NewRequestLogger(r).With(slog.String("func", util.FuncNameParent()))
}
// loggerKeyType is a custom type to avoid key collisions in context values.
type loggerKeyType struct{}
// loggerKey is a unique identifier for retrieving a logger from a context.
var loggerKey = loggerKeyType{}
// MiddlewareLogger creates middleware that injects a logger into the request
// context, enabling subsequent handlers in the chain to log request-specific
// information.
func MiddlewareLogger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger := NewRequestLogger(r)
newCtx := context.WithValue(r.Context(), loggerKey, logger)
next.ServeHTTP(w, r.WithContext(newCtx))
})
}
// Logger attempts to retrieve a logger from the provided context.
//
// It returns the default logger if context is nil or does not contain a logger.
func Logger(ctx context.Context) *slog.Logger {
if ctx == nil {
return slog.Default()
}
if logger, ok := ctx.Value(loggerKey).(*slog.Logger); ok {
return logger
}
return slog.Default()
}