Skip to content

Commit

Permalink
Merge pull request elastic#1 from paylm/feat-trigger-by-date
Browse files Browse the repository at this point in the history
feat: rotator output file by date
  • Loading branch information
paylm authored Mar 3, 2023
2 parents 0ba2aa0 + b961650 commit 54b337c
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions file/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
rotateReasonFileSize
rotateReasonManualTrigger
rotateReasonTimeInterval
rotateReasonDate
)

func (rr rotateReason) String() string {
Expand All @@ -42,6 +43,8 @@ func (rr rotateReason) String() string {
return "manual trigger"
case rotateReasonTimeInterval:
return "time interval"
case rotateReasonDate:
return "date rotate"
default:
return "unknown"
}
Expand All @@ -64,6 +67,7 @@ func newTriggers(rotateOnStartup bool, interval time.Duration, maxSizeBytes uint
if maxSizeBytes > 0 {
triggers = append(triggers, &sizeTrigger{maxSizeBytes: maxSizeBytes, size: 0})
}
triggers = append(triggers, &dateTrigger{})
return triggers
}

Expand All @@ -80,6 +84,26 @@ func (t *initTrigger) TriggerRotation(_ uint) rotateReason {
return rotateReasonNoRotate
}

// date Trigger is triggered by every date.
type dateTrigger struct {
lastDate time.Time
}

func (t *dateTrigger) TriggerRotation(_ uint) rotateReason {
if &(t.lastDate) == nil {
t.lastDate = time.Now()
return rotateReasonNoRotate
}
now := time.Now()
_, _, d1 := now.Date()
_, _, d2 := t.lastDate.Date()
if d1 != d2 {
t.lastDate = now
return rotateReasonDate
}
return rotateReasonNoRotate
}

// sizeTrigger starts a rotation when the file reaches the configured size.
type sizeTrigger struct {
maxSizeBytes uint
Expand Down

0 comments on commit 54b337c

Please sign in to comment.