Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mwdomino committed Jul 31, 2024
1 parent 3677b9b commit f1186c1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
3 changes: 0 additions & 3 deletions docs/user_guide/outputs/file_output.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ outputs:
# list of processors to apply on the message before writing
event-processors:
# file rotation configuration
# all fields are required if enabling file rotation
rotation:
max-size: 100 # size in megabytes
max-age: 30 # max age in days
Expand All @@ -69,5 +68,3 @@ The file output can be used to write to file on the disk, to stdout or to stderr
For a disk file, a file name is required.
For stdout or stderr, only file-type is required.
For rotation, all fields (other than `compress`) are required.
9 changes: 1 addition & 8 deletions pkg/outputs/file/file_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type Config struct {
EnableMetrics bool `mapstructure:"enable-metrics,omitempty"`
Debug bool `mapstructure:"debug,omitempty"`
CalculateLatency bool `mapstructure:"calculate-latency,omitempty"`
Rotation RotationConfig `mapstructure:"rotation,omitempty"`
Rotation rotationConfig `mapstructure:"rotation,omitempty"`
}

type file interface {
Expand Down Expand Up @@ -150,13 +150,6 @@ func (f *File) Init(ctx context.Context, name string, cfg map[string]interface{}
case "stderr":
f.file = os.Stderr
case "rotating":
err := f.cfg.Rotation.validateConfig()
if err != nil {
f.logger.Printf("failed to validate rotating configuration: %v", err)

return err
}

f.file = newRotatingFile(f.cfg)
default:
CRFILE:
Expand Down
24 changes: 10 additions & 14 deletions pkg/outputs/file/rotating_file.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
package file

import (
"fmt"

"gopkg.in/natefinch/lumberjack.v2"
)

// RotationConfig manages configuration around file rotation
type RotationConfig struct {
type rotationConfig struct {
MaxSize int `mapstructure:"max-size,omitempty"`
MaxBackups int `mapstructure:"max-backups,omitempty"`
MaxAge int `mapstructure:"max-age,omitempty"`
Compress bool `mapstructure:"compress,omitempty"`
}

// validateConfig ensures all parameters are supplied
func (rc *RotationConfig) validateConfig() error {
if rc.MaxSize == 0 {
return fmt.Errorf("Rotation.MaxSize is required if using type 'rotating'")
func (r *rotationConfig) SetDefaults() {
if r.MaxSize == 0 {
r.MaxSize = 100
}

if rc.MaxBackups == 0 {
return fmt.Errorf("Rotation.MaxBackups is required if using type 'rotating'")
if r.MaxBackups == 0 {
r.MaxBackups = 3
}

if rc.MaxAge == 0 {
return fmt.Errorf("Rotation.MaxAge is required if using type 'rotating'")
if r.MaxAge == 0 {
r.MaxAge = 30
}

return nil
}

type rotatingFile struct {
Expand All @@ -37,6 +31,8 @@ type rotatingFile struct {

// newRotatingFile initialize the lumberjack instance
func newRotatingFile(cfg *Config) *rotatingFile {
cfg.Rotation.SetDefaults()

lj := lumberjack.Logger{
Filename: cfg.FileName,
MaxSize: cfg.Rotation.MaxSize,
Expand Down

0 comments on commit f1186c1

Please sign in to comment.