-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
105 lines (96 loc) · 2.82 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
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
// 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/tigerwill90/fox/blob/master/LICENSE.txt.
package fox
import (
"errors"
"github.com/tigerwill90/fox/internal/slogpretty"
"log/slog"
"time"
)
// LoggerWithHandler 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.
func LoggerWithHandler(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, ErrNoClientIPStrategy) {
ipStr = c.RemoteIP().String()
} else {
ipStr = "unknown"
}
if location == "" {
log.LogAttrs(
req.Context(),
lvl,
ipStr,
slog.Int("status", c.Writer().Status()),
slog.String("method", req.Method),
slog.String("path", c.Request().URL.String()),
slog.Duration("latency", roundLatency(latency)),
)
} else {
log.LogAttrs(
req.Context(),
lvl,
ipStr,
slog.Int("status", c.Writer().Status()),
slog.String("method", req.Method),
slog.String("path", c.Request().URL.String()),
slog.Duration("latency", roundLatency(latency)),
slog.String("location", location),
)
}
}
}
}
// Logger returns a middleware that logs request information to os.Stdout or os.Stderr (for ERROR level).
// It logs details such as the remote or client IP, HTTP method, request path, status code and latency.
func Logger() MiddlewareFunc {
return LoggerWithHandler(slogpretty.DefaultHandler)
}
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
}
}
func roundLatency(d time.Duration) time.Duration {
switch {
case d < 1*time.Microsecond:
return d.Round(100 * time.Nanosecond)
case d < 1*time.Millisecond:
return d.Round(10 * time.Microsecond)
case d < 10*time.Millisecond:
return d.Round(100 * time.Microsecond)
case d < 100*time.Millisecond:
return d.Round(1 * time.Millisecond)
case d < 1*time.Second:
return d.Round(10 * time.Millisecond)
case d < 10*time.Second:
return d.Round(100 * time.Millisecond)
default:
return d.Round(1 * time.Second)
}
}