feat(test): Allow to capture all messages during test (#13275)

This commit is contained in:
Sven Rebhan 2023-05-17 19:11:52 +02:00 committed by GitHub
parent 92bd5f3776
commit f171d62ac1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 71 additions and 24 deletions

View File

@ -10,73 +10,120 @@ import (
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 `[]`.
errors []string
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{}) {
s := fmt.Sprintf("E! ["+l.Name+"] "+format, args...)
l.Lock()
l.errors = append(l.errors, s)
l.Unlock()
log.Print(s)
l.logf(LevelError, format, args...)
}
// Error logs an error message, patterned after log.Print.
func (l *CaptureLogger) Error(args ...interface{}) {
s := fmt.Sprint(append([]interface{}{"E! [" + l.Name + "] "}, args...)...)
l.Lock()
l.errors = append(l.errors, s)
l.Unlock()
log.Print(s)
l.loga(LevelError, args...)
}
// Debugf logs a debug message, patterned after log.Printf.
func (l *CaptureLogger) Debugf(format string, args ...interface{}) {
log.Printf("D! ["+l.Name+"] "+format, args...)
l.logf(LevelDebug, format, args...)
}
// Debug logs a debug message, patterned after log.Print.
func (l *CaptureLogger) Debug(args ...interface{}) {
log.Print(append([]interface{}{"D! [" + l.Name + "] "}, args...)...)
l.loga(LevelDebug, args...)
}
// Warnf logs a warning message, patterned after log.Printf.
func (l *CaptureLogger) Warnf(format string, args ...interface{}) {
log.Printf("W! ["+l.Name+"] "+format, args...)
l.logf(LevelWarn, format, args...)
}
// Warn logs a warning message, patterned after log.Print.
func (l *CaptureLogger) Warn(args ...interface{}) {
log.Print(append([]interface{}{"W! [" + l.Name + "] "}, args...)...)
l.loga(LevelWarn, args...)
}
// Infof logs an information message, patterned after log.Printf.
func (l *CaptureLogger) Infof(format string, args ...interface{}) {
log.Printf("I! ["+l.Name+"] "+format, args...)
l.logf(LevelInfo, format, args...)
}
// Info logs an information message, patterned after log.Print.
func (l *CaptureLogger) Info(args ...interface{}) {
log.Print(append([]interface{}{"I! [" + l.Name + "] "}, args...)...)
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 {
l.Lock()
defer l.Unlock()
e := append([]string{}, l.errors...)
return e
return l.filter(LevelError)
}
func (l *CaptureLogger) Warnings() []string {
return l.filter(LevelWarn)
}
func (l *CaptureLogger) LastError() string {
l.Lock()
defer l.Unlock()
if len(l.errors) > 0 {
return l.errors[len(l.errors)-1]
for i := len(l.messages) - 1; i >= 0; i-- {
if l.messages[i].Level == LevelError {
return l.messages[i].String()
}
}
return ""
}