Skip to content

Commit

Permalink
Ignore writer error in file output (#4055)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson authored Apr 23, 2018
1 parent 911f0e4 commit f639f99
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions plugins/outputs/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type File struct {
Files []string

writer io.Writer
writers []io.Writer
closers []io.Closer

serializer serializers.Serializer
Expand All @@ -35,15 +35,13 @@ func (f *File) SetSerializer(serializer serializers.Serializer) {
}

func (f *File) Connect() error {
writers := []io.Writer{}

if len(f.Files) == 0 {
f.Files = []string{"stdout"}
}

for _, file := range f.Files {
if file == "stdout" {
writers = append(writers, os.Stdout)
f.writers = append(f.writers, os.Stdout)
} else {
var of *os.File
var err error
Expand All @@ -56,11 +54,10 @@ func (f *File) Connect() error {
if err != nil {
return err
}
writers = append(writers, of)
f.writers = append(f.writers, of)
f.closers = append(f.closers, of)
}
}
f.writer = io.MultiWriter(writers...)
return nil
}

Expand Down Expand Up @@ -90,17 +87,21 @@ func (f *File) Write(metrics []telegraf.Metric) error {
return nil
}

var writeErr error = nil
for _, metric := range metrics {
b, err := f.serializer.Serialize(metric)
if err != nil {
return fmt.Errorf("failed to serialize message: %s", err)
}
_, err = f.writer.Write(b)
if err != nil {
return fmt.Errorf("failed to write message: %s, %s", b, err)

for _, writer := range f.writers {
_, err = writer.Write(b)
if err != nil && writer != os.Stdout {
writeErr = fmt.Errorf("E! failed to write message: %s, %s", b, err)
}
}
}
return nil
return writeErr
}

func init() {
Expand Down

0 comments on commit f639f99

Please sign in to comment.