forked from Securing-DevOps/invoicer-chapter2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
46 lines (40 loc) · 979 Bytes
/
logging.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
package main
import (
"encoding/json"
"log"
"net/http"
)
type accessLog struct {
Type string `json:"type"`
XForwardedFor string `json:"x-forwarded-for"`
Method string `json:"method"`
Proto string `json:"proto"`
URL string `json:"url"`
UserAgent string `json:"user-agent"`
RequestID string `json:"rid"`
}
func (al *accessLog) String() string {
al.Type = "request"
msg, _ := json.Marshal(al)
return string(msg)
}
type appLog struct {
Type string `json:"type"`
RequestID string `json:"rid"`
Message string `json:"msg"`
ErrorCode int `json:"error-code"`
User string `json:"user,omitempty"`
Action string `json:"action,omitempty"`
}
func (al *appLog) String() string {
msg, _ := json.Marshal(al)
return string(msg)
}
func (al *appLog) log(r *http.Request) {
al.Type = "action"
val := r.Context().Value(ctxReqID)
if val != nil {
al.RequestID = val.(string)
}
log.Printf("%s", al.String())
}