-
Notifications
You must be signed in to change notification settings - Fork 5
/
logger.go
103 lines (88 loc) · 2.22 KB
/
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package joblogger
import (
"fmt"
"runtime"
"strings"
"github.com/sirupsen/logrus"
"github.com/c4t-but-s4d/neo/v2/internal/logger"
logspb "github.com/c4t-but-s4d/neo/v2/proto/go/logs"
)
// 1 MB.
const maxMessageLength = 1024 * 1024
func New(exploit string, version int64, team string, sender Sender) *JobLogger {
return &JobLogger{
exploit: exploit,
version: version,
team: team,
sender: sender,
}
}
type JobLogger struct {
exploit string
version int64
team string
sender Sender
}
func (l *JobLogger) Debugf(format string, args ...interface{}) {
l.logProxy(logrus.DebugLevel, format, args...)
msg := fmt.Sprintf(format, args...)
l.sender.Add(l.newLine(msg, "debug"))
}
func (l *JobLogger) Infof(format string, args ...interface{}) {
l.logProxy(logrus.InfoLevel, format, args...)
msg := fmt.Sprintf(format, args...)
l.sender.Add(l.newLine(msg, "info"))
}
func (l *JobLogger) Warningf(format string, args ...interface{}) {
l.logProxy(logrus.WarnLevel, format, args...)
msg := fmt.Sprintf(format, args...)
l.sender.Add(l.newLine(msg, "warning"))
}
func (l *JobLogger) Errorf(format string, args ...interface{}) {
l.logProxy(logrus.ErrorLevel, format, args...)
msg := fmt.Sprintf(format, args...)
l.sender.Add(l.newLine(msg, "error"))
}
func (l *JobLogger) newLine(msg, level string) *logspb.LogLine {
return &logspb.LogLine{
Exploit: l.exploit,
Version: l.version,
Message: sanitizeMessage(msg),
Level: level,
Team: l.team,
}
}
func (l *JobLogger) getLogger() *logrus.Entry {
return logrus.WithFields(logrus.Fields{
"exploit": l.exploit,
"version": l.version,
"team": l.team,
})
}
func (l *JobLogger) logProxy(level logrus.Level, format string, args ...interface{}) {
if logrus.IsLevelEnabled(level) {
l.
getLogger().
WithField(logger.CustomKeyFile, fileInfo(3)).
Logf(level, format, args...)
}
}
func sanitizeMessage(msg string) string {
if len(msg) > maxMessageLength {
msg = msg[:maxMessageLength]
}
return msg
}
func fileInfo(skip int) string {
_, file, line, ok := runtime.Caller(skip)
if !ok {
file = "<???>"
line = 1
} else {
slash := strings.LastIndex(file, "/")
if slash >= 0 {
file = file[slash+1:]
}
}
return fmt.Sprintf("%s:%d", file, line)
}