Skip to content
This repository was archived by the owner on Jul 19, 2021. It is now read-only.

Commit dc2f7d2

Browse files
authored
Merge pull request #55 from tuhao1020/master
add new rotationSize option
2 parents d086d55 + d1277a2 commit dc2f7d2

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type RotateLogs struct {
4545
outFh *os.File
4646
pattern *strftime.Strftime
4747
rotationTime time.Duration
48+
rotationSize int64
4849
rotationCount uint
4950
forceNewFile bool
5051
}

options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const (
1212
optkeyLinkName = "link-name"
1313
optkeyMaxAge = "max-age"
1414
optkeyRotationTime = "rotation-time"
15+
optkeyRotationSize = "rotation-size"
1516
optkeyRotationCount = "rotation-count"
1617
optkeyForceNewFile = "force-new-file"
1718
)
@@ -60,6 +61,12 @@ func WithRotationTime(d time.Duration) Option {
6061
return option.New(optkeyRotationTime, d)
6162
}
6263

64+
// WithRotationSize creates a new Option that sets the
65+
// log file size between rotation.
66+
func WithRotationSize(s int64) Option {
67+
return option.New(optkeyRotationSize, s)
68+
}
69+
6370
// WithRotationCount creates a new Option that sets the
6471
// number of files should be kept before it gets
6572
// purged from the file system.

rotatelogs.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func New(p string, options ...Option) (*RotateLogs, error) {
3737

3838
var clock Clock = Local
3939
rotationTime := 24 * time.Hour
40+
var rotationSize int64
4041
var rotationCount uint
4142
var linkName string
4243
var maxAge time.Duration
@@ -59,6 +60,11 @@ func New(p string, options ...Option) (*RotateLogs, error) {
5960
if rotationTime < 0 {
6061
rotationTime = 0
6162
}
63+
case optkeyRotationSize:
64+
rotationSize = o.Value().(int64)
65+
if rotationSize < 0 {
66+
rotationSize = 0
67+
}
6268
case optkeyRotationCount:
6369
rotationCount = o.Value().(uint)
6470
case optkeyHandler:
@@ -85,6 +91,7 @@ func New(p string, options ...Option) (*RotateLogs, error) {
8591
maxAge: maxAge,
8692
pattern: pattern,
8793
rotationTime: rotationTime,
94+
rotationSize: rotationSize,
8895
rotationCount: rotationCount,
8996
forceNewFile: forceNewFile,
9097
}, nil
@@ -140,6 +147,14 @@ func (rl *RotateLogs) getWriter_nolock(bailOnRotateFail, useGenerationalNames bo
140147
baseFn := rl.genFilename()
141148
filename := baseFn
142149
var forceNewFile bool
150+
151+
fi, err := os.Stat(rl.curFn)
152+
sizeRotation := false
153+
if err == nil && rl.rotationSize > 0 && rl.rotationSize <= fi.Size() {
154+
forceNewFile = true
155+
sizeRotation = true
156+
}
157+
143158
if baseFn != rl.curBaseFn {
144159
generation = 0
145160
// even though this is the first write after calling New(),
@@ -148,7 +163,7 @@ func (rl *RotateLogs) getWriter_nolock(bailOnRotateFail, useGenerationalNames bo
148163
forceNewFile = true
149164
}
150165
} else {
151-
if !useGenerationalNames {
166+
if !useGenerationalNames && !sizeRotation {
152167
// nothing to do
153168
return rl.outFh, nil
154169
}

0 commit comments

Comments
 (0)