diff --git a/internal/config/config.go b/internal/config/config.go index 7f0ab8484..21f5db336 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -149,12 +149,12 @@ type AgentConfig struct { // Log file name, the empty string means to log to stderr. Logfile string `toml:"logfile"` - // The logfile will be rotated when it becomes larger than the specified - // size. When set to 0 no size based rotation is performed. + // The file will be rotated after the time interval specified. When set + // to 0 no time based rotation is performed. LogfileRotationInterval internal.Duration `toml:"logfile_rotation_interval"` - // Maximum number of rotated archives to keep, any older logs are deleted. - // If set to -1, no archives are removed. + // The logfile will be rotated when it becomes larger than the specified + // size. When set to 0 no size based rotation is performed. LogfileRotationMaxSize internal.Size `toml:"logfile_rotation_max_size"` // Maximum number of rotated archives to keep, any older logs are deleted. diff --git a/plugins/outputs/file/README.md b/plugins/outputs/file/README.md index a6c4ac53f..e34f80807 100644 --- a/plugins/outputs/file/README.md +++ b/plugins/outputs/file/README.md @@ -1,15 +1,25 @@ -# file Output Plugin +# File Output Plugin This plugin writes telegraf metrics to files ### Configuration -``` + +```toml [[outputs.file]] ## Files to write to, "stdout" is a specially handled file. files = ["stdout", "/tmp/metrics.out"] - ## If this is defined, files will be rotated by the time.Duration specified - # rotate_max_age = "1m" + ## The file will be rotated after the time interval specified. When set + ## to 0 no time based rotation is performed. + # rotation_interval = "0h" + + ## The logfile will be rotated when it becomes larger than the specified + ## size. When set to 0 no size based rotation is performed. + # rotation_max_size = "0MB" + + ## Maximum number of rotated archives to keep, any older logs are deleted. + ## If set to -1, no archives are removed. + # rotation_max_archives = 5 ## Data format to output. ## Each data format has its own unique set of configuration options, read diff --git a/plugins/outputs/file/file.go b/plugins/outputs/file/file.go index a5eb422b7..11793bc4f 100644 --- a/plugins/outputs/file/file.go +++ b/plugins/outputs/file/file.go @@ -5,21 +5,22 @@ import ( "io" "log" "os" - "time" "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal/rotate" "github.com/influxdata/telegraf/plugins/outputs" "github.com/influxdata/telegraf/plugins/serializers" ) type File struct { - Files []string - RotateMaxAge string - - writer io.Writer - closers []io.Closer + Files []string `toml:"files"` + RotationInterval internal.Duration `toml:"rotation_interval"` + RotationMaxSize internal.Size `toml:"rotation_max_size"` + RotationMaxArchives int `toml:"rotation_max_archives"` + writer io.Writer + closers []io.Closer serializer serializers.Serializer } @@ -27,8 +28,17 @@ var sampleConfig = ` ## Files to write to, "stdout" is a specially handled file. files = ["stdout", "/tmp/metrics.out"] - ## If this is defined, files will be rotated by the time.Duration specified - # rotate_max_age = "1m" + ## The file will be rotated after the time interval specified. When set + ## to 0 no time based rotation is performed. + # rotation_interval = "0d" + + ## The logfile will be rotated when it becomes larger than the specified + ## size. When set to 0 no size based rotation is performed. + # rotation_max_size = "0MB" + + ## Maximum number of rotated archives to keep, any older logs are deleted. + ## If set to -1, no archives are removed. + # rotation_max_archives = 5 ## Data format to output. ## Each data format has its own unique set of configuration options, read @@ -52,23 +62,12 @@ func (f *File) Connect() error { if file == "stdout" { writers = append(writers, os.Stdout) } else { - var of io.WriteCloser - var err error - if f.RotateMaxAge != "" { - maxAge, err := time.ParseDuration(f.RotateMaxAge) - if err != nil { - return err - } - - // Only rotate by file age for now, keep no archives. - of, err = rotate.NewFileWriter(file, maxAge, 0, -1) - } else { - // Just open a normal file - of, err = rotate.NewFileWriter(file, 0, 0, -1) - } + of, err := rotate.NewFileWriter( + file, f.RotationInterval.Duration, f.RotationMaxSize.Size, f.RotationMaxArchives) if err != nil { return err } + writers = append(writers, of) f.closers = append(f.closers, of) } @@ -107,7 +106,7 @@ func (f *File) Write(metrics []telegraf.Metric) error { _, err = f.writer.Write(b) if err != nil { - writeErr = fmt.Errorf("E! failed to write message: %s, %s", b, err) + writeErr = fmt.Errorf("E! [outputs.file] failed to write message: %v", err) } }