forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcapturelog.go
135 lines (111 loc) · 2.95 KB
/
capturelog.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
package testutil
import (
"fmt"
"log"
"sync"
"github.com/influxdata/telegraf"
)
var _ telegraf.Logger = &CaptureLogger{}
const (
LevelError = 'E'
LevelWarn = 'W'
LevelInfo = 'I'
LevelDebug = 'D'
)
type Entry struct {
Level byte
Name string
Text string
}
func (e *Entry) String() string {
return fmt.Sprintf("%c! [%s] %s", e.Level, e.Name, e.Text)
}
// CaptureLogger defines a logging structure for plugins.
type CaptureLogger struct {
Name string // Name is the plugin name, will be printed in the `[]`.
messages []Entry
sync.Mutex
}
func (l *CaptureLogger) print(msg Entry) {
l.Lock()
l.messages = append(l.messages, msg)
l.Unlock()
log.Print(msg.String())
}
func (l *CaptureLogger) logf(level byte, format string, args ...any) {
l.print(Entry{level, l.Name, fmt.Sprintf(format, args...)})
}
func (l *CaptureLogger) loga(level byte, args ...any) {
l.print(Entry{level, l.Name, fmt.Sprint(args...)})
}
// Errorf logs an error message, patterned after log.Printf.
func (l *CaptureLogger) Errorf(format string, args ...interface{}) {
l.logf(LevelError, format, args...)
}
// Error logs an error message, patterned after log.Print.
func (l *CaptureLogger) Error(args ...interface{}) {
l.loga(LevelError, args...)
}
// Debugf logs a debug message, patterned after log.Printf.
func (l *CaptureLogger) Debugf(format string, args ...interface{}) {
l.logf(LevelDebug, format, args...)
}
// Debug logs a debug message, patterned after log.Print.
func (l *CaptureLogger) Debug(args ...interface{}) {
l.loga(LevelDebug, args...)
}
// Warnf logs a warning message, patterned after log.Printf.
func (l *CaptureLogger) Warnf(format string, args ...interface{}) {
l.logf(LevelWarn, format, args...)
}
// Warn logs a warning message, patterned after log.Print.
func (l *CaptureLogger) Warn(args ...interface{}) {
l.loga(LevelWarn, args...)
}
// Infof logs an information message, patterned after log.Printf.
func (l *CaptureLogger) Infof(format string, args ...interface{}) {
l.logf(LevelInfo, format, args...)
}
// Info logs an information message, patterned after log.Print.
func (l *CaptureLogger) Info(args ...interface{}) {
l.loga(LevelInfo, args...)
}
func (l *CaptureLogger) Messages() []Entry {
l.Lock()
msgs := make([]Entry, len(l.messages))
copy(msgs, l.messages)
l.Unlock()
return msgs
}
func (l *CaptureLogger) filter(level byte) []string {
l.Lock()
defer l.Unlock()
var msgs []string
for _, m := range l.messages {
if m.Level == level {
msgs = append(msgs, m.String())
}
}
return msgs
}
func (l *CaptureLogger) Errors() []string {
return l.filter(LevelError)
}
func (l *CaptureLogger) Warnings() []string {
return l.filter(LevelWarn)
}
func (l *CaptureLogger) LastError() string {
l.Lock()
defer l.Unlock()
for i := len(l.messages) - 1; i >= 0; i-- {
if l.messages[i].Level == LevelError {
return l.messages[i].String()
}
}
return ""
}
func (l *CaptureLogger) Clear() {
l.Lock()
defer l.Unlock()
l.messages = make([]Entry, 0)
}