github.com/influxdata/telegraf@v1.30.3/testutil/capturelog.go (about)

     1  package testutil
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"sync"
     7  
     8  	"github.com/influxdata/telegraf"
     9  )
    10  
    11  var _ telegraf.Logger = &CaptureLogger{}
    12  
    13  const (
    14  	LevelError = 'E'
    15  	LevelWarn  = 'W'
    16  	LevelInfo  = 'I'
    17  	LevelDebug = 'D'
    18  )
    19  
    20  type Entry struct {
    21  	Level byte
    22  	Name  string
    23  	Text  string
    24  }
    25  
    26  func (e *Entry) String() string {
    27  	return fmt.Sprintf("%c! [%s] %s", e.Level, e.Name, e.Text)
    28  }
    29  
    30  // CaptureLogger defines a logging structure for plugins.
    31  type CaptureLogger struct {
    32  	Name     string // Name is the plugin name, will be printed in the `[]`.
    33  	messages []Entry
    34  	sync.Mutex
    35  }
    36  
    37  func (l *CaptureLogger) print(msg Entry) {
    38  	l.Lock()
    39  	l.messages = append(l.messages, msg)
    40  	l.Unlock()
    41  	log.Print(msg.String())
    42  }
    43  
    44  func (l *CaptureLogger) logf(level byte, format string, args ...any) {
    45  	l.print(Entry{level, l.Name, fmt.Sprintf(format, args...)})
    46  }
    47  
    48  func (l *CaptureLogger) loga(level byte, args ...any) {
    49  	l.print(Entry{level, l.Name, fmt.Sprint(args...)})
    50  }
    51  
    52  // Errorf logs an error message, patterned after log.Printf.
    53  func (l *CaptureLogger) Errorf(format string, args ...interface{}) {
    54  	l.logf(LevelError, format, args...)
    55  }
    56  
    57  // Error logs an error message, patterned after log.Print.
    58  func (l *CaptureLogger) Error(args ...interface{}) {
    59  	l.loga(LevelError, args...)
    60  }
    61  
    62  // Debugf logs a debug message, patterned after log.Printf.
    63  func (l *CaptureLogger) Debugf(format string, args ...interface{}) {
    64  	l.logf(LevelDebug, format, args...)
    65  }
    66  
    67  // Debug logs a debug message, patterned after log.Print.
    68  func (l *CaptureLogger) Debug(args ...interface{}) {
    69  	l.loga(LevelDebug, args...)
    70  }
    71  
    72  // Warnf logs a warning message, patterned after log.Printf.
    73  func (l *CaptureLogger) Warnf(format string, args ...interface{}) {
    74  	l.logf(LevelWarn, format, args...)
    75  }
    76  
    77  // Warn logs a warning message, patterned after log.Print.
    78  func (l *CaptureLogger) Warn(args ...interface{}) {
    79  	l.loga(LevelWarn, args...)
    80  }
    81  
    82  // Infof logs an information message, patterned after log.Printf.
    83  func (l *CaptureLogger) Infof(format string, args ...interface{}) {
    84  	l.logf(LevelInfo, format, args...)
    85  }
    86  
    87  // Info logs an information message, patterned after log.Print.
    88  func (l *CaptureLogger) Info(args ...interface{}) {
    89  	l.loga(LevelInfo, args...)
    90  }
    91  
    92  func (l *CaptureLogger) NMessages() int {
    93  	l.Lock()
    94  	defer l.Unlock()
    95  	return len(l.messages)
    96  }
    97  
    98  func (l *CaptureLogger) Messages() []Entry {
    99  	l.Lock()
   100  	msgs := make([]Entry, len(l.messages))
   101  	copy(msgs, l.messages)
   102  	l.Unlock()
   103  	return msgs
   104  }
   105  
   106  func (l *CaptureLogger) filter(level byte) []string {
   107  	l.Lock()
   108  	defer l.Unlock()
   109  	var msgs []string
   110  	for _, m := range l.messages {
   111  		if m.Level == level {
   112  			msgs = append(msgs, m.String())
   113  		}
   114  	}
   115  	return msgs
   116  }
   117  
   118  func (l *CaptureLogger) Errors() []string {
   119  	return l.filter(LevelError)
   120  }
   121  
   122  func (l *CaptureLogger) Warnings() []string {
   123  	return l.filter(LevelWarn)
   124  }
   125  
   126  func (l *CaptureLogger) LastError() string {
   127  	l.Lock()
   128  	defer l.Unlock()
   129  	for i := len(l.messages) - 1; i >= 0; i-- {
   130  		if l.messages[i].Level == LevelError {
   131  			return l.messages[i].String()
   132  		}
   133  	}
   134  	return ""
   135  }
   136  
   137  func (l *CaptureLogger) Clear() {
   138  	l.Lock()
   139  	defer l.Unlock()
   140  	l.messages = make([]Entry, 0)
   141  }