-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapplication_log.go
58 lines (47 loc) · 1.23 KB
/
application_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
package pluto
import (
"encoding/json"
"time"
"github.com/google/uuid"
"go.uber.org/zap"
)
// ApplicationLogger
//
// TODO:
// 2. Add other log levels, error, info ..
var ApplicationLogger = ApplicationLogCollector{NewChannel("APPLICATION_LOGGER", 10)}
type ApplicationLogCollector struct {
Channel Channel
}
type ApplicationLog struct {
Message string `json:"message"`
Extra map[string]any `json:"extra,omitempty"`
Level string `json:"level"`
CreatedAt time.Time `json:"created_at"`
}
func (l *ApplicationLogCollector) Debug(log ApplicationLog) {
log.Level = "Debug"
l.Log(log)
}
func (l *ApplicationLogCollector) Warning(log ApplicationLog) {
log.Level = "Warning"
l.Log(log)
}
func (l *ApplicationLogCollector) Error(log ApplicationLog) {
log.Level = "Error"
l.Log(log)
}
func (l *ApplicationLogCollector) Log(log ApplicationLog) {
log.CreatedAt = time.Now()
// TODO: Do not convert log to bytes. The subscriber may do it.
b, err := json.Marshal(log)
if err != nil {
Log.Error("Marshalling ApplicationLog", zap.Error(err))
return
}
l.Channel.Publish(&InternalProcessable{
ID: uuid.New(), // TODO: Do not generate uuid every time!
Body: b,
CreatedAt: time.Now(),
})
}