-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
107 lines (98 loc) · 3.28 KB
/
logger.go
File metadata and controls
107 lines (98 loc) · 3.28 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
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
// Copyright 2022 Sylvain Müller. All rights reserved.
// Mount of this source code is governed by a Apache-2.0 license that can be found
// at https://github.com/fox-toolkit/fox/blob/master/LICENSE.txt.
package fox
import (
"errors"
"log/slog"
"time"
)
// Keys for "built-in" logger attribute for the logger middleware.
const (
// LoggerStatusKey is the key used by the built-in logger middleware for the HTTP response status code
// when the log method is called. The associated [slog.Value] is a string.
LoggerStatusKey = "status"
// LoggerMethodKey is the key used by the built-in logger middleware for the HTTP request method.
// The associated [slog.Value] is a string.
LoggerMethodKey = "method"
// LoggerHostKey is the key used by the built-in logger middleware for the request host.
// The associated [slog.Value] is a string.
LoggerHostKey = "host"
// LoggerPathKey is the key used by the built-in logger middleware for the request path.
// The associated [slog.Value] is a string.
LoggerPathKey = "path"
// LoggerLatencyKey is the key used by the built-in logger middleware for the request processing duration.
// The associated [slog.Value] is a time.Duration.
LoggerLatencyKey = "latency"
// LoggerSizeKey is the key used by the built-in logger middleware for the response body size.
// The associated [slog.Value] is an int.
LoggerSizeKey = "size"
// LoggerLocationKey is the key used by the built-in logger middleware for redirect location header.
// The associated [slog.Value] is a string.
LoggerLocationKey = "location"
)
// Logger returns a middleware that logs request information using the provided [slog.Handler].
// It logs details such as the remote or client IP, HTTP method, request path, status code and latency.
// Status codes are logged at different levels: 2xx at INFO, 3xx at DEBUG (with Location header if present),
// 4xx at WARN, and 5xx at ERROR.
func Logger(handler slog.Handler) MiddlewareFunc {
log := slog.New(handler)
return func(next HandlerFunc) HandlerFunc {
return func(c *Context) {
start := time.Now()
next(c)
latency := time.Since(start)
req := c.Request()
lvl := level(c.Writer().Status())
var location string
if lvl.Level() == slog.LevelDebug {
location = c.Writer().Header().Get(HeaderLocation)
}
var ipStr string
ip, err := c.ClientIP()
if err == nil {
ipStr = ip.String()
} else if errors.Is(err, ErrNoClientIPResolver) {
ipStr = c.RemoteIP().String()
} else {
ipStr = "unknown"
}
l := log.With(
slog.Int(LoggerStatusKey, c.Writer().Status()),
slog.String(LoggerMethodKey, c.Method()),
slog.String(LoggerHostKey, c.Host()),
slog.String(LoggerPathKey, c.Path()),
slog.Int(LoggerSizeKey, c.Writer().Size()),
slog.Duration(LoggerLatencyKey, latency),
)
if location == "" {
l.Log(
req.Context(),
lvl,
ipStr,
)
return
}
l.LogAttrs(
req.Context(),
lvl,
ipStr,
slog.String(LoggerLocationKey, location),
)
}
}
}
func level(status int) slog.Level {
switch {
case status >= 200 && status < 300:
return slog.LevelInfo
case status >= 300 && status < 400:
return slog.LevelDebug
case status >= 400 && status < 500:
return slog.LevelWarn
case status >= 500:
return slog.LevelError
default:
return slog.LevelInfo
}
}