-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_entries.go
229 lines (203 loc) · 4.89 KB
/
log_entries.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package logger
import (
"bytes"
"fmt"
"log"
"net"
"os"
"time"
)
// msgQueue is message queue channel
type msgQueue struct {
messagesToSend chan *bytes.Buffer
}
// Enqueue will enqueue the message
func (m *msgQueue) Enqueue(msg *bytes.Buffer) {
m.messagesToSend <- msg
}
// PushFront push the message to front
func (m *msgQueue) PushFront(msg *bytes.Buffer) {
messages := []*bytes.Buffer{msg}
for {
select {
case msg := <-m.messagesToSend:
messages = append(messages, msg)
default:
for _, msg := range messages {
m.messagesToSend <- msg
}
return
}
}
}
// LogClient configuration
type LogClient struct {
conn *net.TCPConn
endpoint string
messages msgQueue
port string
retryDelay time.Duration
token string
}
// NewLogEntriesClient new client
func NewLogEntriesClient(token, endpoint, port string) (*LogClient, error) {
l := &LogClient{
endpoint: endpoint,
port: port,
retryDelay: RetryDelay,
token: token,
}
l.messages.messagesToSend = make(chan *bytes.Buffer, 1000)
if err := l.Connect(); err != nil {
return l, err
}
return l, nil
}
// Connect will connect to Log Entries
func (l *LogClient) Connect() error {
if l.conn != nil {
_ = l.conn.Close() // close the connection, don't care about the error
}
l.conn = nil
addr, err := net.ResolveTCPAddr("tcp", l.endpoint+":"+l.port)
if err != nil {
return err
}
var conn *net.TCPConn
if conn, err = net.DialTCP("tcp", nil, addr); err != nil {
l.retryDelay *= 2
if l.retryDelay > MaxRetryDelay {
l.retryDelay = MaxRetryDelay
}
return err
}
_ = conn.SetNoDelay(true)
// if err != nil {
// return err
// }
_ = conn.SetKeepAlive(true)
// if err != nil {
// return err
// }
l.conn = conn
l.retryDelay = RetryDelay
return nil
}
// ProcessQueue process the queue
func (l *LogClient) ProcessQueue() {
for msg := range l.messages.messagesToSend {
if l.conn == nil {
l.messages.PushFront(msg)
time.Sleep(l.retryDelay)
if err := l.Connect(); err != nil {
log.Println("failed reconnecting to log provider", err)
continue
}
}
if _, err := l.conn.Write(msg.Bytes()); err != nil {
l.messages.PushFront(msg)
log.Println("failed to write to log provider", err)
time.Sleep(l.retryDelay)
if err = l.Connect(); err != nil {
log.Println("failed reconnecting to log provider after failing to write", err)
continue
}
}
}
}
// write will write the data to the que
func (l *LogClient) write(data string) {
var buff bytes.Buffer
buff.WriteString(l.token)
buff.WriteByte(' ')
buff.WriteString(data)
l.messages.Enqueue(&buff)
}
// Panic overloads built-in method
func (l *LogClient) Panic(v ...interface{}) {
var buff bytes.Buffer
buff.WriteString(l.token)
buff.WriteByte(' ')
buff.WriteString(fmt.Sprintln(v...))
_ = l.sendOne(&buff)
time.Sleep(2 * time.Second)
os.Exit(1)
}
// Panicln overloads built-in method
func (l *LogClient) Panicln(v ...interface{}) {
var buff bytes.Buffer
buff.WriteString(l.token)
buff.WriteByte(' ')
buff.WriteString(fmt.Sprintln(v...))
_ = l.sendOne(&buff)
time.Sleep(2 * time.Second)
os.Exit(1)
}
// Panicf overloads built-in method
func (l *LogClient) Panicf(format string, v ...interface{}) {
var buff bytes.Buffer
buff.WriteString(l.token)
buff.WriteByte(' ')
buff.WriteString(fmt.Sprintf(format, v...))
_ = l.sendOne(&buff)
time.Sleep(2 * time.Second)
os.Exit(1)
}
// Print overloads built-in method
func (l *LogClient) Print(v ...interface{}) {
l.write(fmt.Sprintln(v...))
}
// Println overloads built-in method
func (l *LogClient) Println(v ...interface{}) {
l.write(fmt.Sprintln(v...))
}
// Printf overloads built-in method
func (l *LogClient) Printf(format string, v ...interface{}) {
l.write(fmt.Sprintf(format, v...))
}
// Fatal overloads built-in method
func (l *LogClient) Fatal(v ...interface{}) {
var buff bytes.Buffer
buff.WriteString(l.token)
buff.WriteByte(' ')
buff.WriteString(fmt.Sprintln(v...))
_ = l.sendOne(&buff)
time.Sleep(2 * time.Second)
os.Exit(1)
}
// Fatalln overloads built-in method
func (l *LogClient) Fatalln(v ...interface{}) {
var buff bytes.Buffer
buff.WriteString(l.token)
buff.WriteByte(' ')
buff.WriteString(fmt.Sprintln(v...))
_ = l.sendOne(&buff)
time.Sleep(2 * time.Second)
os.Exit(1)
}
// Fatalf overloads built-in method
func (l *LogClient) Fatalf(format string, v ...interface{}) {
var buff bytes.Buffer
buff.WriteString(l.token)
buff.WriteByte(' ')
buff.WriteString(fmt.Sprintf(format, v...))
_ = l.sendOne(&buff)
time.Sleep(2 * time.Second)
os.Exit(1)
}
// sendOne sends one log
func (l *LogClient) sendOne(msg *bytes.Buffer) (err error) {
if l.conn == nil {
if err = l.Connect(); err != nil {
log.Println(msg.String())
log.Println("failed reconnecting to log provider", err)
return
}
}
if _, err = l.conn.Write(msg.Bytes()); err != nil {
log.Println(msg.String())
log.Println("failed to write to log provider", err)
return
}
return
}