-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp_logger.go
204 lines (148 loc) · 4.82 KB
/
http_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package gosrv
import (
"io"
"net"
"net/http"
"time"
"strings"
"fmt"
)
// Interface for value fetching functions. Time represents when the request
// was received. The given ResponseWriter should always be a gosrv.Response
// when used in a gosrv.Mux.
type LogValueFunc func(time.Time, http.ResponseWriter, *http.Request)string;
// Map of logFormat keywords to functions. More may be added at need.
var LogValueMap = map[string]LogValueFunc {
"$RemoteAddr": lvRemoteAddr,
"$Protocol": lvProtocol,
"$RequestTime": lvDuration,
"$Time": lvRequestTime,
"$RequestMethod": lvRequestMethod,
"$BodyBytes": lvResponseBytes,
"$RemoteUser": lvRemoteUser,
"$RequestUri": lvRequestUri,
"$RequestPath": lvRequestPath,
"$Request": lvRequestFirstLine,
"$Status": lvResponseStatus,
"$HttpReferer": lvReferer,
"$HttpUserAgent": lvUserAgent,
}
var DefaultLogFormat =
"$RemoteAddr - $RemoteUser $Time \"$Request\" $Status $BodyBytes \"$HttpReferer\" \"$HttpUserAgent\""
var DefaultTimeFormat = "[02/Jan/2006:15:04:05 -0700]"
// The logger interface for gosrv.Mux.
type HttpLogger interface {
io.Writer
SetLogFormat(format string)
SetTimeFormat(time_format string)
SetWriter(wr io.Writer)
Println(i ...interface{}) (int, error)
Printf(format string, i ...interface{}) (int, error)
Log(t time.Time, wr http.ResponseWriter, req *http.Request)
}
type httpLogger struct {
logFormat string
timeFormat string
keys []string
writer io.Writer
}
func NewHttpLogger(wr io.Writer, formats ...string) HttpLogger {
log_format := DefaultLogFormat
time_format := DefaultTimeFormat
if len(formats) > 0 { log_format = formats[0] }
if len(formats) > 1 { time_format = formats[1] }
l := &httpLogger{timeFormat: time_format, writer: wr}
l.SetLogFormat(log_format)
return l
}
func (l *httpLogger) SetLogFormat(log_format string) {
keys := []string{}
for k, _ := range LogValueMap {
if strings.Contains(log_format, k) {
keys = append(keys, k)
}
}
l.logFormat = log_format
l.keys = keys
}
func (l *httpLogger) SetTimeFormat(time_format string) {
l.timeFormat = time_format
}
func (l *httpLogger) SetWriter(wr io.Writer) {
l.writer = wr
}
func (l *httpLogger) Write(bytes []byte) (int, error) {
return l.writer.Write(bytes)
}
func (l *httpLogger) Printf(format string, i ...interface{}) (int, error) {
return fmt.Fprintf(l, format, i...)
}
func (l *httpLogger) Println(i ...interface{}) (int, error) {
return fmt.Fprintln(l, i...)
}
func (l *httpLogger) Log(t time.Time, wr http.ResponseWriter, req *http.Request) {
repl := []string{}
for _, k := range l.keys {
repl = append(repl, k, LogValueMap[k](t, wr, req))
}
r := strings.NewReplacer(repl...)
line := r.Replace(l.logFormat) + "\n"
l.Write([]byte(line))
}
func lvRemoteAddr(t time.Time, wr http.ResponseWriter, req *http.Request) string {
host, _, err := net.SplitHostPort(req.RemoteAddr)
if err != nil { host = req.RemoteAddr }
return host
}
func lvDuration(t time.Time, wr http.ResponseWriter, req *http.Request) string {
duration := time.Since(t) / time.Microsecond
return fmt.Sprintf("%d", duration)
}
func lvProtocol(t time.Time, wr http.ResponseWriter, req *http.Request) string {
return req.Proto
}
func lvRequestTime(t time.Time, wr http.ResponseWriter, req *http.Request) string {
return t.Format(DefaultTimeFormat)
}
func lvRequestMethod(t time.Time, wr http.ResponseWriter, req *http.Request) string {
return req.Method
}
func lvResponseBytes(t time.Time, wr http.ResponseWriter, req *http.Request) string {
b := "-"
if res, ok := wr.(*Response); ok {
b = fmt.Sprintf("%d", res.ContentLength())
if b == "0" { b = "-" }
}
return b
}
func lvRemoteUser(t time.Time, wr http.ResponseWriter, req *http.Request) string {
remoteUser := "-"
if req.URL.User != nil && req.URL.User.Username() != "" {
remoteUser = req.URL.User.Username()
} else if len(req.Header["Remote-User"]) > 0 {
remoteUser = req.Header["Remote-User"][0]
}
return remoteUser
}
func lvRequestPath(t time.Time, wr http.ResponseWriter, req *http.Request) string {
return req.URL.Path
}
func lvRequestUri(t time.Time, wr http.ResponseWriter, req *http.Request) string {
return req.RequestURI
}
func lvRequestFirstLine(t time.Time, wr http.ResponseWriter, req *http.Request) string {
return lvRequestMethod(t, wr, req) + " " +
lvRequestUri(t, wr, req) + " " +
lvProtocol(t, wr, req)
}
func lvResponseStatus(t time.Time, wr http.ResponseWriter, req *http.Request) string {
if res, ok := wr.(*Response); ok {
return fmt.Sprintf("%d", res.Status) }
return "-"
}
func lvReferer(t time.Time, wr http.ResponseWriter, req *http.Request) string {
return req.Referer()
}
func lvUserAgent(t time.Time, wr http.ResponseWriter, req *http.Request) string {
return req.UserAgent()
}