Skip to content

Commit

Permalink
file: allow configuration of log filename extension (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
efd6 authored Jul 25, 2022
1 parent 7bba8bb commit dbea8c3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Add option to `file.NewFileRotator` to allow setting file extension. #68

### Changed

### Deprecated
Expand All @@ -14,6 +16,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Fix filename logging in `file.NewFileRotator`. #68

## [0.2.9]

### Changed
Expand Down
21 changes: 16 additions & 5 deletions file/rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
package file

import (
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"sync"
"time"

"errors"
)

const (
Expand Down Expand Up @@ -55,6 +54,7 @@ type Rotator struct {
triggers []trigger

filename string
extension string
maxSizeBytes uint
maxBackups uint
interval time.Duration
Expand Down Expand Up @@ -84,6 +84,14 @@ func MaxSizeBytes(n uint) RotatorOption {
}
}

// Extension configures the file extension to use for the log file.
// The default is "ndjson".
func Extension(ext string) RotatorOption {
return func(r *Rotator) {
r.extension = ext
}
}

// MaxBackups configures the maximum number of backup files to save (not
// counting the active file). The upper limit is 1024 on this value is.
// The default is 7.
Expand Down Expand Up @@ -142,6 +150,8 @@ func WithClock(clock clock) RotatorOption {
// NewFileRotator returns a new Rotator.
func NewFileRotator(filename string, options ...RotatorOption) (*Rotator, error) {
r := &Rotator{
filename: filename,
extension: "ndjson",
maxSizeBytes: 10 * 1024 * 1024, // 10 MiB
maxBackups: 7,
permissions: 0600,
Expand All @@ -168,7 +178,7 @@ func NewFileRotator(filename string, options ...RotatorOption) (*Rotator, error)
return nil, errors.New("the minimum time interval for log rotation is 1 second")
}

r.rot = newDateRotater(r.log, filename, r.clock)
r.rot = newDateRotater(r.log, filename, r.extension, r.clock)

shouldRotateOnStart := r.rotateOnStartup
if _, err := os.Stat(r.rot.ActiveFile()); os.IsNotExist(err) {
Expand All @@ -180,6 +190,7 @@ func NewFileRotator(filename string, options ...RotatorOption) (*Rotator, error)
if r.log != nil {
r.log.Debugw("Initialized file rotator",
"filename", r.filename,
"extension", r.extension,
"max_size_bytes", r.maxSizeBytes,
"max_backups", r.maxBackups,
"permissions", r.permissions,
Expand Down Expand Up @@ -408,12 +419,12 @@ type dateRotator struct {
logOrderCache map[string]logOrder
}

func newDateRotater(log Logger, filename string, clock clock) rotater {
func newDateRotater(log Logger, filename, extension string, clock clock) rotater {
d := &dateRotator{
log: log,
clock: clock,
filenamePrefix: filename + "-",
extension: ".ndjson",
extension: "." + extension,
format: DateFormat,
logOrderCache: make(map[string]logOrder),
}
Expand Down
38 changes: 38 additions & 0 deletions file/rotator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,44 @@ func TestRotate(t *testing.T) {
AssertDirContents(t, dir, secondFile, thirdFile)
}

func TestRotateExtension(t *testing.T) {
dir := t.TempDir()

const (
logname = "beatname"
extension = "log"
)
filename := filepath.Join(dir, logname)

c := &testClock{time.Date(2021, 11, 11, 0, 0, 0, 0, time.Local)}
r, err := file.NewFileRotator(filename, file.Extension(extension), file.MaxBackups(1), file.WithClock(c))
if err != nil {
t.Fatal(err)
}
defer r.Close()

WriteMsg(t, r)

firstFile := fmt.Sprintf("%s-%s."+extension, logname, c.Now().Format(file.DateFormat))
AssertDirContents(t, dir, firstFile)

c.time = time.Date(2021, 11, 13, 0, 0, 0, 0, time.Local)
secondFile := fmt.Sprintf("%s-%s."+extension, logname, c.Now().Format(file.DateFormat))

Rotate(t, r)
WriteMsg(t, r)

AssertDirContents(t, dir, firstFile, secondFile)

c.time = time.Date(2021, 11, 15, 0, 0, 0, 0, time.Local)
thirdFile := fmt.Sprintf("%s-%s."+extension, logname, c.Now().Format(file.DateFormat))

Rotate(t, r)
WriteMsg(t, r)

AssertDirContents(t, dir, secondFile, thirdFile)
}

func CreateFile(t *testing.T, filename string) {
t.Helper()
f, err := os.Create(filename)
Expand Down

0 comments on commit dbea8c3

Please sign in to comment.