Skip to content

Commit dbea8c3

Browse files
authored
file: allow configuration of log filename extension (#68)
1 parent 7bba8bb commit dbea8c3

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66

77
### Added
88

9+
- Add option to `file.NewFileRotator` to allow setting file extension. #68
10+
911
### Changed
1012

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

1517
### Fixed
1618

19+
- Fix filename logging in `file.NewFileRotator`. #68
20+
1721
## [0.2.9]
1822

1923
### Changed

file/rotator.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
package file
1919

2020
import (
21+
"errors"
2122
"fmt"
2223
"os"
2324
"path/filepath"
2425
"sort"
2526
"strconv"
2627
"sync"
2728
"time"
28-
29-
"errors"
3029
)
3130

3231
const (
@@ -55,6 +54,7 @@ type Rotator struct {
5554
triggers []trigger
5655

5756
filename string
57+
extension string
5858
maxSizeBytes uint
5959
maxBackups uint
6060
interval time.Duration
@@ -84,6 +84,14 @@ func MaxSizeBytes(n uint) RotatorOption {
8484
}
8585
}
8686

87+
// Extension configures the file extension to use for the log file.
88+
// The default is "ndjson".
89+
func Extension(ext string) RotatorOption {
90+
return func(r *Rotator) {
91+
r.extension = ext
92+
}
93+
}
94+
8795
// MaxBackups configures the maximum number of backup files to save (not
8896
// counting the active file). The upper limit is 1024 on this value is.
8997
// The default is 7.
@@ -142,6 +150,8 @@ func WithClock(clock clock) RotatorOption {
142150
// NewFileRotator returns a new Rotator.
143151
func NewFileRotator(filename string, options ...RotatorOption) (*Rotator, error) {
144152
r := &Rotator{
153+
filename: filename,
154+
extension: "ndjson",
145155
maxSizeBytes: 10 * 1024 * 1024, // 10 MiB
146156
maxBackups: 7,
147157
permissions: 0600,
@@ -168,7 +178,7 @@ func NewFileRotator(filename string, options ...RotatorOption) (*Rotator, error)
168178
return nil, errors.New("the minimum time interval for log rotation is 1 second")
169179
}
170180

171-
r.rot = newDateRotater(r.log, filename, r.clock)
181+
r.rot = newDateRotater(r.log, filename, r.extension, r.clock)
172182

173183
shouldRotateOnStart := r.rotateOnStartup
174184
if _, err := os.Stat(r.rot.ActiveFile()); os.IsNotExist(err) {
@@ -180,6 +190,7 @@ func NewFileRotator(filename string, options ...RotatorOption) (*Rotator, error)
180190
if r.log != nil {
181191
r.log.Debugw("Initialized file rotator",
182192
"filename", r.filename,
193+
"extension", r.extension,
183194
"max_size_bytes", r.maxSizeBytes,
184195
"max_backups", r.maxBackups,
185196
"permissions", r.permissions,
@@ -408,12 +419,12 @@ type dateRotator struct {
408419
logOrderCache map[string]logOrder
409420
}
410421

411-
func newDateRotater(log Logger, filename string, clock clock) rotater {
422+
func newDateRotater(log Logger, filename, extension string, clock clock) rotater {
412423
d := &dateRotator{
413424
log: log,
414425
clock: clock,
415426
filenamePrefix: filename + "-",
416-
extension: ".ndjson",
427+
extension: "." + extension,
417428
format: DateFormat,
418429
logOrderCache: make(map[string]logOrder),
419430
}

file/rotator_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,44 @@ func TestRotate(t *testing.T) {
254254
AssertDirContents(t, dir, secondFile, thirdFile)
255255
}
256256

257+
func TestRotateExtension(t *testing.T) {
258+
dir := t.TempDir()
259+
260+
const (
261+
logname = "beatname"
262+
extension = "log"
263+
)
264+
filename := filepath.Join(dir, logname)
265+
266+
c := &testClock{time.Date(2021, 11, 11, 0, 0, 0, 0, time.Local)}
267+
r, err := file.NewFileRotator(filename, file.Extension(extension), file.MaxBackups(1), file.WithClock(c))
268+
if err != nil {
269+
t.Fatal(err)
270+
}
271+
defer r.Close()
272+
273+
WriteMsg(t, r)
274+
275+
firstFile := fmt.Sprintf("%s-%s."+extension, logname, c.Now().Format(file.DateFormat))
276+
AssertDirContents(t, dir, firstFile)
277+
278+
c.time = time.Date(2021, 11, 13, 0, 0, 0, 0, time.Local)
279+
secondFile := fmt.Sprintf("%s-%s."+extension, logname, c.Now().Format(file.DateFormat))
280+
281+
Rotate(t, r)
282+
WriteMsg(t, r)
283+
284+
AssertDirContents(t, dir, firstFile, secondFile)
285+
286+
c.time = time.Date(2021, 11, 15, 0, 0, 0, 0, time.Local)
287+
thirdFile := fmt.Sprintf("%s-%s."+extension, logname, c.Now().Format(file.DateFormat))
288+
289+
Rotate(t, r)
290+
WriteMsg(t, r)
291+
292+
AssertDirContents(t, dir, secondFile, thirdFile)
293+
}
294+
257295
func CreateFile(t *testing.T, filename string) {
258296
t.Helper()
259297
f, err := os.Create(filename)

0 commit comments

Comments
 (0)