-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
99 lines (85 loc) · 1.9 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
package settings
import (
"bytes"
"encoding/json"
"fmt"
jwt "github.com/dgrijalva/jwt-go"
"io/ioutil"
"log"
"net/http"
"regexp"
"time"
)
type Log struct {
Time string
Message string
ResponseCode int
Action string
Method string
URI string
User string
RequestData string
}
func maskPassword(str string) string {
re := regexp.MustCompile("\"password\":\\s*\"([^\"]+)\"")
return re.ReplaceAllString(str, "\"password\": \"*\"")
}
func getUserEmail(token_str string) string {
if token_str == "" {
return ""
}
conf = GetConfig()
token, _ := jwt.Parse(token_str, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", t.Header["alg"])
}
return []byte(conf.TokenSecret), nil
})
if token.Valid {
email, _ := token.Claims.(jwt.MapClaims)["sub"].(string)
return email
} else {
return ""
}
}
func WriteLog(r *http.Request, code int, erro error, action string) {
if r.Method == "OPTIONS" {
return
}
msg := "OK"
if erro != nil {
msg = erro.Error()
}
var bodyBytes []byte
if r.Body != nil {
bodyBytes, _ = ioutil.ReadAll(r.Body)
}
bodyString := string(bodyBytes)
bodyString = maskPassword(bodyString)
// if len(bodyString) > 100 {
// bodyString = bodyString[0:99]
// }
token := r.Header.Get("x-auth-token")
email := getUserEmail(token)
logs := Log{
time.Now().Format("2006-01-02 15:04:05"), msg, code, action, r.Method, r.RequestURI, email, bodyString,
}
b, err := json.Marshal(logs)
if err != nil {
log.Fatal(err)
}
conf = GetConfig()
if conf.LogIndent {
var out bytes.Buffer
json.Indent(&out, b, "", " ")
fmt.Printf("%s\n", out.String())
} else {
fmt.Printf("%s\n", string(b))
}
}
func WriteInfoLog(v interface{}) {
log.Printf("[Info] %v\n", v)
}
func WriteDebugLog(v interface{}) {
log.Printf("[Debug] %v\n", v)
}