-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_logger.go
81 lines (66 loc) · 1.89 KB
/
redis_logger.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
package qmq
import (
"fmt"
"google.golang.org/protobuf/types/known/anypb"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
type RedisLogger struct {
name string
producer Producer
logLevel int
}
func NewRedisLogger(name string, connection *RedisConnection, logLevel int, maxLength int64) Logger {
return &RedisLogger{
name: name,
producer: NewRedisProducer(connection, &RedisProducerConfig{
Topic: name + ":logs",
Length: maxLength,
Transformers: []Transformer{},
}),
logLevel: logLevel,
}
}
func (l *RedisLogger) Log(level Log_LogLevelEnum, message string, args ...interface{}) {
if int(level) < l.logLevel {
return
}
logMsg := &Log{
Level: level,
Message: fmt.Sprintf(message, args...),
Timestamp: timestamppb.Now(),
Application: l.name,
}
fmt.Printf("%s | %s | %s | %s\n", logMsg.Timestamp.AsTime().String(), logMsg.Application, logMsg.Level.String(), logMsg.Message)
content, _ := anypb.New(logMsg)
l.producer.Push(&Message{
Header: &Header{
Id: new(DefaultMessageIdGenerator).Generate(),
Source: l.name,
Destination: l.name + ":logs",
Subject: content.TypeUrl,
},
Content: content,
})
}
func (l *RedisLogger) Trace(message string, args ...interface{}) {
l.Log(Log_TRACE, message, args...)
}
func (l *RedisLogger) Debug(message string, args ...interface{}) {
l.Log(Log_DEBUG, message, args...)
}
func (l *RedisLogger) Advise(message string, args ...interface{}) {
l.Log(Log_ADVISE, message, args...)
}
func (l *RedisLogger) Warn(message string, args ...interface{}) {
l.Log(Log_WARN, message, args...)
}
func (l *RedisLogger) Error(message string, args ...interface{}) {
l.Log(Log_ERROR, message, args...)
}
func (l *RedisLogger) Panic(message string, args ...interface{}) {
l.Log(Log_PANIC, message, args...)
panic(message)
}
func (l *RedisLogger) Close() {
l.producer.Close()
}