-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlogger.go
158 lines (134 loc) · 3.51 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package axios4go
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
)
type LogLevel int
const (
LevelNone LogLevel = iota
LevelError
LevelInfo
LevelDebug
)
type Logger interface {
LogRequest(*http.Request, LogLevel)
LogResponse(*http.Response, []byte, time.Duration, LogLevel)
LogError(error, LogLevel)
SetLevel(LogLevel)
}
type LogOptions struct {
Level LogLevel
MaxBodyLength int
MaskHeaders []string
Output io.Writer
TimeFormat string
IncludeBody bool
IncludeHeaders bool
}
type DefaultLogger struct {
options LogOptions
}
func NewDefaultLogger(options LogOptions) *DefaultLogger {
if options.Output == nil {
options.Output = os.Stdout
}
if options.TimeFormat == "" {
options.TimeFormat = time.RFC3339
}
if options.MaxBodyLength == 0 {
options.MaxBodyLength = 1000
}
return &DefaultLogger{options: options}
}
func (l *DefaultLogger) SetLevel(level LogLevel) {
l.options.Level = level
}
func (l *DefaultLogger) LogRequest(req *http.Request, level LogLevel) {
if level > l.options.Level {
return
}
var buf strings.Builder
timestamp := time.Now().Format(l.options.TimeFormat)
fmt.Fprintf(&buf, "[%s] REQUEST: %s %s\n", timestamp, req.Method, req.URL)
if l.options.IncludeHeaders {
buf.WriteString("Headers:\n")
for key, vals := range req.Header {
if l.isHeaderMasked(key) {
fmt.Fprintf(&buf, " %s: [MASKED]\n", key)
} else {
fmt.Fprintf(&buf, " %s: %s\n", key, strings.Join(vals, ", "))
}
}
}
if l.options.IncludeBody && req.Body != nil {
body, err := io.ReadAll(req.Body)
if err == nil {
req.Body = io.NopCloser(bytes.NewBuffer(body))
if len(body) > l.options.MaxBodyLength {
fmt.Fprintf(&buf, "Body: (truncated) %s...\n", body[:l.options.MaxBodyLength])
} else {
fmt.Fprintf(&buf, "Body: %s\n", body)
}
}
}
fmt.Fprintln(l.options.Output, buf.String())
}
func (l *DefaultLogger) LogResponse(resp *http.Response, body []byte, duration time.Duration, level LogLevel) {
if level > l.options.Level {
return
}
var buf strings.Builder
timestamp := time.Now().Format(l.options.TimeFormat)
fmt.Fprintf(&buf, "[%s] RESPONSE: %d %s (%.2fms)\n",
timestamp, resp.StatusCode, resp.Status, float64(duration.Microseconds())/1000)
if l.options.IncludeHeaders {
buf.WriteString("Headers:\n")
for key, vals := range resp.Header {
if l.isHeaderMasked(key) {
fmt.Fprintf(&buf, " %s: [MASKED]\n", key)
} else {
fmt.Fprintf(&buf, " %s: %s\n", key, strings.Join(vals, ", "))
}
}
}
if l.options.IncludeBody && body != nil {
if len(body) > l.options.MaxBodyLength {
fmt.Fprintf(&buf, "Body: (truncated) %s...\n", body[:l.options.MaxBodyLength])
} else {
fmt.Fprintf(&buf, "Body: %s\n", body)
}
}
fmt.Fprintln(l.options.Output, buf.String())
}
func (l *DefaultLogger) LogError(err error, level LogLevel) {
if level > l.options.Level {
return
}
timestamp := time.Now().Format(l.options.TimeFormat)
fmt.Fprintf(l.options.Output, "[%s] ERROR: %v\n", timestamp, err)
}
func (l *DefaultLogger) isHeaderMasked(header string) bool {
header = strings.ToLower(header)
for _, masked := range l.options.MaskHeaders {
if strings.ToLower(masked) == header {
return true
}
}
return false
}
func NewLogger(level LogLevel) Logger {
return NewDefaultLogger(LogOptions{
Level: level,
MaxBodyLength: 1000,
MaskHeaders: []string{"Authorization", "Cookie", "Set-Cookie"},
Output: os.Stdout,
TimeFormat: time.RFC3339,
IncludeBody: true,
IncludeHeaders: true,
})
}