forked from mercari/gaurun
/
log.go
162 lines (146 loc) · 3.52 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
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
package gaurun
import (
"bytes"
"encoding/json"
"fmt"
"github.com/Sirupsen/logrus"
"os"
"strconv"
"sync"
"sync/atomic"
"time"
)
type LogReq struct {
Type string `json:"type"`
Time string `json:"time"`
URI string `json:"uri"`
Method string `json:"method"`
Proto string `json:"proto"`
ContentLength int64 `json:"content_length"`
}
type LogPushEntry struct {
Type string `json:"type"`
Time string `json:"time"`
ID uint64 `json:"id"`
Platform string `json:"platform"`
Token string `json:"token"`
Message string `json:"message"`
Ptime float64 `json:"ptime"`
Error string `json:"error"`
// Android
CollapseKey string `json:"collapse_key,omitempty"`
DelayWhileIdle bool `json:"delay_while_idle,omitempty"`
TimeToLive int `json:"time_to_live,omitempty"`
// iOS
Badge int `json:"badge,omitempty"`
Sound string `json:"sound,omitempty"`
Expiry int `json:"expiry,omitempty"`
}
type GaurunFormatter struct {
}
func (f *GaurunFormatter) Format(entry *logrus.Entry) ([]byte, error) {
b := &bytes.Buffer{}
fmt.Fprintf(b, "[%s] ", entry.Level.String())
fmt.Fprintf(b, "%s", entry.Message)
b.WriteByte('\n')
return b.Bytes(), nil
}
func InitLog() *logrus.Logger {
return logrus.New()
}
func SetLogOut(log *logrus.Logger, outString string) error {
switch outString {
case "stdout":
log.Out = os.Stdout
case "stderr":
log.Out = os.Stderr
default:
f, err := os.OpenFile(outString, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return err
}
log.Out = f
}
return nil
}
func SetLogLevel(log *logrus.Logger, levelString string) error {
level, err := logrus.ParseLevel(levelString)
if err != nil {
return err
}
log.Level = level
return nil
}
func LogAcceptedRequest(uri, method, proto string, length int64) {
log := &LogReq{
Type: "accepted-request",
Time: time.Now().Format("2006/01/02 15:04:05 MST"),
URI: uri,
Method: method,
Proto: proto,
ContentLength: length,
}
logJSON, err := json.Marshal(log)
if err != nil {
LogError.Error("Marshaling JSON error")
return
}
LogAccess.Info(string(logJSON))
}
func LogPush(id uint64, status, token string, ptime float64, req RequestGaurunNotification, errPush error) {
var plat string
switch req.Platform {
case PlatFormIos:
plat = "ios"
case PlatFormAndroid:
plat = "android"
}
ptime3 := fmt.Sprintf("%.3f", ptime)
ptime, _ = strconv.ParseFloat(ptime3, 64)
errMsg := ""
if errPush != nil {
errMsg = errPush.Error()
}
log := &LogPushEntry{
Type: status,
Time: time.Now().Format("2006/01/02 15:04:05 MST"),
ID: id,
Platform: plat,
Token: token,
Message: req.Message,
Ptime: ptime,
Error: errMsg,
CollapseKey: req.CollapseKey,
DelayWhileIdle: req.DelayWhileIdle,
TimeToLive: req.TimeToLive,
Badge: req.Badge,
Sound: req.Sound,
Expiry: req.Expiry,
}
logJSON, err := json.Marshal(log)
if err != nil {
LogError.Error("Marshaling JSON error")
return
}
switch status {
case StatusAcceptedPush:
fallthrough
case StatusSucceededPush:
LogAccess.Info(string(logJSON))
case StatusFailedPush:
LogError.Error(string(logJSON))
}
}
func numberingPush() uint64 {
var result uint64
// nubmering push
OnceNumbering.Do(func() {
WgNumbering = new(sync.WaitGroup)
})
WgNumbering.Add(1)
atomic.AddUint64(&SeqID, 1)
result = SeqID
WgNumbering.Done()
WgNumbering.Wait()
return result
}