-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlog.go
70 lines (57 loc) · 1.39 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
package coralogix
import (
"encoding/json"
"sync"
)
// Log describe record format for Coralogix API
type Log struct {
Timestamp float64 `json:"timestamp"` // Log record timestamp
Severity uint `json:"severity"` // Log record severity level
Text string `json:"text"` // Log record message
Category string `json:"category"` // Log record category
ClassName string `json:"className"` // Log record class name
MethodName string `json:"methodName"` // Log record method name
ThreadID string `json:"threadId"` // Thread ID
size uint64
}
// Size calculate log record length in bytes
func (Record *Log) Size() uint64 {
if Record.size == 0 {
JSONRecord, err := json.Marshal(Record)
if err != nil {
return 0
}
Record.size = uint64(len(string(JSONRecord)))
}
return Record.size
}
type LogBuffer struct {
buffer []Log
size uint64
lock sync.Mutex
}
func (lb *LogBuffer) Append(log Log) {
lb.lock.Lock()
defer lb.lock.Unlock()
lb.size += log.Size()
lb.buffer = append(lb.buffer, log)
}
func (lb *LogBuffer) Size() uint64 {
return lb.size
}
func (lb *LogBuffer) Len() int {
return len(lb.buffer)
}
func (lb *LogBuffer) Slice(i int) []Log {
lb.lock.Lock()
defer lb.lock.Unlock()
if i > len(lb.buffer) {
i = len(lb.buffer)
}
slice := lb.buffer[:i]
lb.buffer = lb.buffer[i:]
for _, l := range slice {
lb.size -= l.Size()
}
return slice
}