-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicklog.go
239 lines (210 loc) · 4.93 KB
/
quicklog.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
230
231
232
233
234
235
236
237
238
239
package quicklog
import (
"fmt"
"sort"
"strings"
"sync"
"time"
)
// TODO: perhaps we should add a redis backend..
// if we do though, lets keep track of "instance" id so we can correlate logs
// between services
// Quicklog is a simple in-memory log service for quick at-a-glance logs
type Quicklog interface {
Info(group string, message string, v ...any)
Warn(group string, message string, v ...any)
Groups() []string
Logs(group string) []LogEntry
ToMap(timezone string, withExactTime bool, group ...string) map[string][]string
}
type LogEntry interface {
Level() string
Group() string
Message() string
Time() time.Time
TimeAgo(timezone ...string) string
Count() uint32
FormattedMessage(timezone string, withExactTime ...bool) string
}
type quicklog struct {
logs map[string][]logEntry
numEntries int
mu sync.RWMutex
}
func NewQuicklog(numEntries ...int) Quicklog {
n := 40
if len(numEntries) > 0 {
n = numEntries[0]
}
if n < 1 {
n = 1
}
if n > 500 {
// its intended to be small, so operations are fast and memory usage is low
n = 500
}
return &quicklog{
logs: make(map[string][]logEntry),
numEntries: n,
}
}
type logEntry struct {
group string
message string
level string
time time.Time
count uint32
}
func (w *quicklog) Info(group, message string, v ...any) {
w.log("INFO", group, message, v...)
}
func (w *quicklog) Warn(group, message string, v ...any) {
w.log("WARN", group, message, v...)
}
func (w *quicklog) log(level, group, message string, v ...any) {
w.mu.Lock()
defer w.mu.Unlock()
if len(group) == 0 {
return
}
g, ok := w.logs[group]
if !ok {
g = make([]logEntry, 0, w.numEntries)
}
msg := fmt.Sprintf(message, v...)
if len(msg) == 0 {
return
}
if len(msg) > 1000 {
// truncate to 1000 characters per message
msg = msg[:1000]
}
// First check if the message is already in the log, and if so, increment the count
// and update the time
for i, entry := range g {
if entry.message == msg && entry.level == level {
entry.count++
entry.time = time.Now().UTC()
g[i] = entry
w.logs[group] = g
sort.Slice(g, func(i, j int) bool {
return g[i].time.After(g[j].time)
})
return
}
}
// Add the new entry
newLen := len(g) + 1
if newLen > w.numEntries {
newLen = w.numEntries
}
newG := make([]logEntry, newLen)
newEntry := logEntry{
group: group,
message: msg,
level: level,
time: time.Now().UTC(),
count: 1,
}
newG[0] = newEntry
if len(g) > 0 {
copy(newG[1:], g[:newLen-1])
}
w.logs[group] = newG
}
func (w *quicklog) Reset(group ...string) {
w.mu.Lock()
defer w.mu.Unlock()
if len(group) > 0 {
delete(w.logs, group[0])
} else {
w.logs = make(map[string][]logEntry)
}
}
func (w *quicklog) Groups() []string {
w.mu.RLock()
defer w.mu.RUnlock()
keys := make([]string, 0, len(w.logs))
for k := range w.logs {
keys = append(keys, k)
}
return keys
}
func (w *quicklog) Logs(group string) []LogEntry {
w.mu.RLock()
defer w.mu.RUnlock()
out := make([]LogEntry, 0, len(w.logs[group]))
for _, entry := range w.logs[group] {
out = append(out, entry)
}
return out
}
func (w *quicklog) ToMap(timezone string, withExactTime bool, group ...string) map[string][]string {
m := make(map[string][]string)
for k, v := range w.logs {
if len(group) > 0 && !strings.HasPrefix(k, group[0]) {
continue
}
for _, entry := range v {
m[k] = append(m[k], entry.FormattedMessage(timezone, withExactTime))
}
}
return m
}
func (l logEntry) Group() string {
return l.group
}
func (l logEntry) Message() string {
return l.message
}
func (l logEntry) Level() string {
return l.level
}
func (l logEntry) Time() time.Time {
return l.time
}
func (l logEntry) TimeAgo(timezone ...string) string {
var err error
loc := time.UTC
if len(timezone) > 0 {
loc, err = time.LoadLocation(timezone[0])
if err != nil {
loc = time.UTC
}
}
duration := time.Since(l.time.In(loc))
if duration < time.Minute {
return fmt.Sprintf("%ds ago", int(duration.Seconds()))
} else if duration < time.Hour {
seconds := int(duration.Seconds()) % 60
return fmt.Sprintf("%dm %ds ago", int(duration.Minutes()), seconds)
} else if duration < 24*time.Hour {
hours := int(duration.Hours())
minutes := int(duration.Minutes()) % 60
if minutes == 0 {
return fmt.Sprintf("%dh ago", hours)
}
return fmt.Sprintf("%dh %dm ago", hours, minutes)
}
return l.time.In(loc).Format(time.RFC822)
}
func (l logEntry) Count() uint32 {
return l.count
}
func (l logEntry) FormattedMessage(timezone string, withExactTime ...bool) string {
loc, err := time.LoadLocation(timezone)
if err != nil {
loc = time.UTC
}
var out string
if len(withExactTime) > 0 && withExactTime[0] {
out = fmt.Sprintf("%s - [%s] %s", l.time.In(loc).Format(time.RFC822), l.level, l.message)
} else {
out = fmt.Sprintf("%s - [%s] %s", l.TimeAgo(timezone), l.level, l.message)
}
if l.count > 1 {
return fmt.Sprintf("%s [x%d]", out, l.count)
} else {
return out
}
}