Ignore writer error in file output (#4055)

This commit is contained in:
Daniel Nelson 2018-04-23 15:08:04 -07:00 committed by GitHub
parent 96120a6fc5
commit c277161858
1 changed files with 11 additions and 10 deletions

View File

@ -13,7 +13,7 @@ import (
type File struct { type File struct {
Files []string Files []string
writer io.Writer writers []io.Writer
closers []io.Closer closers []io.Closer
serializer serializers.Serializer serializer serializers.Serializer
@ -35,15 +35,13 @@ func (f *File) SetSerializer(serializer serializers.Serializer) {
} }
func (f *File) Connect() error { func (f *File) Connect() error {
writers := []io.Writer{}
if len(f.Files) == 0 { if len(f.Files) == 0 {
f.Files = []string{"stdout"} f.Files = []string{"stdout"}
} }
for _, file := range f.Files { for _, file := range f.Files {
if file == "stdout" { if file == "stdout" {
writers = append(writers, os.Stdout) f.writers = append(f.writers, os.Stdout)
} else { } else {
var of *os.File var of *os.File
var err error var err error
@ -56,11 +54,10 @@ func (f *File) Connect() error {
if err != nil { if err != nil {
return err return err
} }
writers = append(writers, of) f.writers = append(f.writers, of)
f.closers = append(f.closers, of) f.closers = append(f.closers, of)
} }
} }
f.writer = io.MultiWriter(writers...)
return nil return nil
} }
@ -90,17 +87,21 @@ func (f *File) Write(metrics []telegraf.Metric) error {
return nil return nil
} }
var writeErr error = nil
for _, metric := range metrics { for _, metric := range metrics {
b, err := f.serializer.Serialize(metric) b, err := f.serializer.Serialize(metric)
if err != nil { if err != nil {
return fmt.Errorf("failed to serialize message: %s", err) return fmt.Errorf("failed to serialize message: %s", err)
} }
_, err = f.writer.Write(b)
if err != nil { for _, writer := range f.writers {
return fmt.Errorf("failed to write message: %s, %s", b, err) _, 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() { func init() {