Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added RollingInterval, to rotate file every x sec. #101

Open
wants to merge 3 commits into
base: v2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions lumberjack.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ type Logger struct {
// based on age.
MaxAge int `json:"maxage" yaml:"maxage"`

// RollingInterval is the number of seconds before rotating to a new log file.
// the old log files will be deleted by the MaxAge & MaxBackups properties as usual.
// if the rolling interval is 0 the feature is off, default is 0.
RollingInterval int64 `json:"rollinginterval" yaml:"rollinginterval"`

// MaxBackups is the maximum number of old log files to retain. The default
// is to retain all old log files (though MaxAge may still cause them to get
// deleted.)
Expand All @@ -105,11 +110,11 @@ type Logger struct {

// Compress determines if the rotated log files should be compressed
// using gzip. The default is not to perform compression.
Compress bool `json:"compress" yaml:"compress"`

size int64
file *os.File
mu sync.Mutex
Compress bool `json:"compress" yaml:"compress"`
createdAt int64
size int64
file *os.File
mu sync.Mutex

millCh chan bool
startMill sync.Once
Expand Down Expand Up @@ -155,6 +160,12 @@ func (l *Logger) Write(p []byte) (n int, err error) {
}
}

if l.exceedsRollingInterval() {
if err := l.rotate(); err != nil {
return 0, err
}
}

n, err = l.file.Write(p)
l.size += int64(n)

Expand Down Expand Up @@ -238,6 +249,7 @@ func (l *Logger) openNew() error {
}
l.file = f
l.size = 0
l.createdAt = time.Now().Unix()
return nil
}

Expand Down Expand Up @@ -449,6 +461,11 @@ func (l *Logger) max() int64 {
return int64(l.MaxSize) * int64(megabyte)
}

// exceedsRollingInterval checks if the log file age exceeds the rolling interval
func (l *Logger) exceedsRollingInterval() bool {
return l.RollingInterval > 0 && time.Now().Unix()-l.createdAt >= l.RollingInterval
}

// dir returns the directory for the current filename.
func (l *Logger) dir() string {
return filepath.Dir(l.filename())
Expand Down
30 changes: 30 additions & 0 deletions lumberjack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,36 @@ func TestDefaultFilename(t *testing.T) {
existsWithContent(filename, b, t)
}

func TestRollingInterval(t *testing.T) {
currentTime = fakeTime
bCount := 0
dir := makeTempDir("RollingInterval", t)
defer os.RemoveAll(dir)
filename := logFile(dir)
l := &Logger{
Filename: filename,
MaxSize: 100,
RollingInterval: 2,
}
defer l.Close()
b := []byte("boo!")
allBytes := append(b, b...)
for i := 0; i < 2; i++ {
n, err := l.Write(b)
isNil(err, t)
bCount += n
}
equals(len(allBytes), bCount, t)
existsWithContent(filename, allBytes, t)
fileCount(dir, 1, t)
l.createdAt = l.createdAt - 2
n, err := l.Write(b)
isNil(err, t)
equals(len(b), n, t)
existsWithContent(l.filename(), b, t)
fileCount(dir, 2, t)
}

func TestAutoRotate(t *testing.T) {
currentTime = fakeTime
megabyte = 1
Expand Down