-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to label inputs for logging (#6207)
- Loading branch information
1 parent
bc52592
commit 5c8d0e3
Showing
22 changed files
with
475 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package models | ||
|
||
import ( | ||
"log" | ||
"reflect" | ||
|
||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/selfstat" | ||
) | ||
|
||
// Logger defines a logging structure for plugins. | ||
type Logger struct { | ||
Errs selfstat.Stat | ||
Name string // Name is the plugin name, will be printed in the `[]`. | ||
} | ||
|
||
// Errorf logs an error message, patterned after log.Printf. | ||
func (l *Logger) Errorf(format string, args ...interface{}) { | ||
l.Errs.Incr(1) | ||
log.Printf("E! ["+l.Name+"] "+format, args...) | ||
} | ||
|
||
// Error logs an error message, patterned after log.Print. | ||
func (l *Logger) Error(args ...interface{}) { | ||
l.Errs.Incr(1) | ||
log.Print(append([]interface{}{"E! [" + l.Name + "] "}, args...)...) | ||
} | ||
|
||
// Debugf logs a debug message, patterned after log.Printf. | ||
func (l *Logger) Debugf(format string, args ...interface{}) { | ||
log.Printf("D! ["+l.Name+"] "+format, args...) | ||
} | ||
|
||
// Debug logs a debug message, patterned after log.Print. | ||
func (l *Logger) Debug(args ...interface{}) { | ||
log.Print(append([]interface{}{"D! [" + l.Name + "] "}, args...)...) | ||
} | ||
|
||
// Warnf logs a warning message, patterned after log.Printf. | ||
func (l *Logger) Warnf(format string, args ...interface{}) { | ||
log.Printf("W! ["+l.Name+"] "+format, args...) | ||
} | ||
|
||
// Warn logs a warning message, patterned after log.Print. | ||
func (l *Logger) Warn(args ...interface{}) { | ||
log.Print(append([]interface{}{"W! [" + l.Name + "] "}, args...)...) | ||
} | ||
|
||
// Infof logs an information message, patterned after log.Printf. | ||
func (l *Logger) Infof(format string, args ...interface{}) { | ||
log.Printf("I! ["+l.Name+"] "+format, args...) | ||
} | ||
|
||
// Info logs an information message, patterned after log.Print. | ||
func (l *Logger) Info(args ...interface{}) { | ||
log.Print(append([]interface{}{"I! [" + l.Name + "] "}, args...)...) | ||
} | ||
|
||
// logName returns the log-friendly name/type. | ||
func logName(pluginType, name, alias string) string { | ||
if alias == "" { | ||
return pluginType + "." + name | ||
} | ||
return pluginType + "." + name + "::" + alias | ||
} | ||
|
||
func setLogIfExist(i interface{}, log telegraf.Logger) { | ||
valI := reflect.ValueOf(i) | ||
|
||
if valI.Type().Kind() != reflect.Ptr { | ||
valI = reflect.New(reflect.TypeOf(i)) | ||
} | ||
|
||
field := valI.Elem().FieldByName("Log") | ||
if !field.IsValid() { | ||
return | ||
} | ||
|
||
switch field.Type().String() { | ||
case "telegraf.Logger": | ||
if field.CanSet() { | ||
field.Set(reflect.ValueOf(log)) | ||
} | ||
} | ||
|
||
return | ||
} |
Oops, something went wrong.