forked from rancher/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access.go
45 lines (36 loc) · 1.03 KB
/
access.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
// Package common Note: inspiration for this from https://gist.github.com/cespare/3985516
package common
import (
"net/http"
"strconv"
"strings"
"time"
"github.com/golang/glog"
)
const (
logFile = "access_log.txt"
)
type accessLog struct {
ip, method, uri, protocol, host string
elapsedTime time.Duration
}
func LogAccess(w http.ResponseWriter, req *http.Request, duration time.Duration) {
clientIP := req.RemoteAddr
if colon := strings.LastIndex(clientIP, ":"); colon != -1 {
clientIP = clientIP[:colon]
}
record := &accessLog{
ip: clientIP,
method: req.Method,
uri: req.RequestURI,
protocol: req.Proto,
host: req.Host,
elapsedTime: duration,
}
writeAccessLog(record)
}
func writeAccessLog(record *accessLog) {
logRecord := "" + record.ip + " " + record.protocol + " " + record.method + ": " + record.uri + ", host: " + record.host + " (load time: " + strconv.FormatFloat(record.elapsedTime.Seconds(), 'f', 5, 64) + " seconds)"
glog.Infoln(logRecord)
glog.Flush()
}