github.com/Jeffail/benthos/v3@v3.65.0/public/service/logger.go (about) 1 package service 2 3 import ( 4 "fmt" 5 6 "github.com/Jeffail/benthos/v3/lib/log" 7 ) 8 9 // Logger allows plugin authors to write custom logs from components that are 10 // exported the same way as native Benthos logs. It's safe to pass around a nil 11 // pointer for testing components. 12 type Logger struct { 13 m log.Modular 14 } 15 16 func newReverseAirGapLogger(l log.Modular) *Logger { 17 return &Logger{l} 18 } 19 20 // Debugf logs a debug message using fmt.Sprintf when args are specified. 21 func (l *Logger) Debugf(template string, args ...interface{}) { 22 if l == nil { 23 return 24 } 25 l.m.Debugf(template, args...) 26 } 27 28 // Debug logs a debug message. 29 func (l *Logger) Debug(message string) { 30 if l == nil { 31 return 32 } 33 l.m.Debugln(message) 34 } 35 36 // Infof logs an info message using fmt.Sprintf when args are specified. 37 func (l *Logger) Infof(template string, args ...interface{}) { 38 if l == nil { 39 return 40 } 41 l.m.Infof(template, args...) 42 } 43 44 // Info logs an info message. 45 func (l *Logger) Info(message string) { 46 if l == nil { 47 return 48 } 49 l.m.Infoln(message) 50 } 51 52 // Warnf logs a warning message using fmt.Sprintf when args are specified. 53 func (l *Logger) Warnf(template string, args ...interface{}) { 54 if l == nil { 55 return 56 } 57 l.m.Warnf(template, args...) 58 } 59 60 // Warn logs a warning message. 61 func (l *Logger) Warn(message string) { 62 if l == nil { 63 return 64 } 65 l.m.Warnln(message) 66 } 67 68 // Errorf logs an error message using fmt.Sprintf when args are specified. 69 func (l *Logger) Errorf(template string, args ...interface{}) { 70 if l == nil { 71 return 72 } 73 l.m.Errorf(template, args...) 74 } 75 76 // Error logs an error message. 77 func (l *Logger) Error(message string) { 78 if l == nil { 79 return 80 } 81 l.m.Errorln(message) 82 } 83 84 // With adds a variadic set of fields to a logger. Each field must consist 85 // of a string key and a value of any type. An odd number of key/value pairs 86 // will therefore result in malformed log messages, but should never panic. 87 func (l *Logger) With(keyValuePairs ...interface{}) *Logger { 88 if l == nil { 89 return nil 90 } 91 fields := map[string]string{} 92 for i := 0; i < (len(keyValuePairs) - 1); i += 2 { 93 key, ok := keyValuePairs[i].(string) 94 if !ok { 95 key = fmt.Sprintf("%v", keyValuePairs[i]) 96 } 97 value, ok := keyValuePairs[i+1].(string) 98 if !ok { 99 value = fmt.Sprintf("%v", keyValuePairs[i+1]) 100 } 101 fields[key] = value 102 } 103 lg := l.m.WithFields(fields) 104 return &Logger{lg} 105 }