forked from allegro/akubra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
84 lines (76 loc) · 2.57 KB
/
log.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
package httphandler
import (
"fmt"
"net/http"
"time"
"github.com/allegro/akubra/log"
)
// AccessMessageData holds all important informations
// about http roundtrip
type AccessMessageData struct {
Method string `json:"method"`
Host string `json:"host"`
Path string `json:"path"`
UserAgent string `json:"useragent"`
StatusCode int `json:"status"`
Duration float64 `json:"duration_ms"`
RespErr string `json:"error"`
ReqID string `json:"reqID"`
Time string `json:"ts"`
}
// String produces data in csv format with fields in following order:
// Method, Host, Path, UserAgent, StatusCode, Duration, RespErr)
func (amd AccessMessageData) String() string {
return fmt.Sprintf("%q, %q, %q, %q, %d, %f, %q",
amd.Method, amd.Host, amd.Path, amd.UserAgent,
amd.StatusCode, amd.Duration, amd.RespErr)
}
// NewAccessLogMessage creates new AccessMessageData
func NewAccessLogMessage(req http.Request,
statusCode int, duration float64, respErr string) *AccessMessageData {
ts := time.Now().Format(time.RFC3339Nano)
reqID, _ := req.Context().Value(log.ContextreqIDKey).(string)
return &AccessMessageData{
req.Method,
req.Host,
req.URL.Path,
req.Header.Get("User-Agent"),
statusCode, duration, respErr,
reqID, ts}
}
// ScanCSVAccessLogMessage will scan csv string and return AccessMessageData.
// Returns fmt.SScanf error if matching failed
func ScanCSVAccessLogMessage(csvstr string) (AccessMessageData, error) {
amd := AccessMessageData{}
_, err := fmt.Sscanf(csvstr, "%q, %q, %q, %q, %d, %f, %q", &amd.Method, &amd.Host,
&amd.Path, &amd.UserAgent, &amd.StatusCode, &amd.Duration,
&amd.RespErr, &amd.Time)
return amd, err
}
// SyncLogMessageData holds all important informations
// about replication errors
type SyncLogMessageData struct {
Method string `json:"method"`
FailedHost string `json:"failedhost"`
Path string `json:"path"`
SuccessHost string `json:"successhost"`
UserAgent string `json:"useragent"`
// ContentLength if negative means no content length header provided
ContentLength int64 `json:"content-length"`
AccessKey string `json:"access-key"`
ErrorMsg string `json:"error"`
ReqID string `json:"reqID"`
Time string `json:"ts"`
}
// String produces data in csv format with fields in following order:
// Method, Host, Path, UserAgent, StatusCode, Duration, RespErr)
func (slmd SyncLogMessageData) String() string {
return fmt.Sprintf("%q, %q, %q, %q, %q, %d, %q",
slmd.Method,
slmd.FailedHost,
slmd.Path,
slmd.SuccessHost,
slmd.UserAgent,
slmd.ContentLength,
slmd.ErrorMsg)
}