Skip to content

Commit

Permalink
👔 up: rotatefile - 20% probability trigger clean on write log
Browse files Browse the repository at this point in the history
- add new config func: WithDebugMode
  • Loading branch information
inhere committed Mar 24, 2024
1 parent ec256be commit e492570
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 26 deletions.
5 changes: 5 additions & 0 deletions rotatefile/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,8 @@ func WithFilepath(logfile string) ConfigFn {
c.Filepath = logfile
}
}

// WithDebugMode setting for debug mode
func WithDebugMode(c *Config) {
c.DebugMode = true
}
1 change: 1 addition & 0 deletions rotatefile/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func TestRotateTime_String(t *testing.T) {
assert.Eq(t, "Every 15 Minutes", rotatefile.RotateTime(timex.OneMinSec*15).String())
assert.Eq(t, "Every 5 Minutes", rotatefile.RotateTime(timex.OneMinSec*5).String())
assert.Eq(t, "Every 3 Seconds", rotatefile.RotateTime(3).String())
assert.Eq(t, "Every 2 Day", rotatefile.RotateTime(timex.OneDaySec*2).String())
}

func TestRotateTime_FirstCheckTime_Round(t *testing.T) {
Expand Down
8 changes: 0 additions & 8 deletions rotatefile/rotatefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
package rotatefile

import (
"fmt"
"io"
"os"
)

// RotateWriter interface
Expand Down Expand Up @@ -58,9 +56,3 @@ const (
// DefaultBackTime default backup time for old files. default keep a week.
DefaultBackTime uint = 24 * 7
)

func printErrln(pfx string, err error) {
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, pfx, err)
}
}
7 changes: 7 additions & 0 deletions rotatefile/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rotatefile

import (
"compress/gzip"
"fmt"
"io"
"io/fs"
"os"
Expand All @@ -11,6 +12,12 @@ import (

const compressSuffix = ".gz"

func printErrln(pfx string, err error) {
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, pfx, err)
}
}

func compressFile(srcPath, dstPath string) error {
srcFile, err := os.OpenFile(srcPath, os.O_RDONLY, 0)
if err != nil {
Expand Down
59 changes: 41 additions & 18 deletions rotatefile/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rotatefile
import (
"fmt"
"io/fs"
"math/rand"
"os"
"path"
"sort"
Expand Down Expand Up @@ -107,6 +108,11 @@ func (d *Writer) Close() error {
return d.close(true)
}

// MustClose the writer. alias of Close(), but will panic if has error.
func (d *Writer) MustClose() {
printErrln("close writer -", d.close(true))
}

func (d *Writer) close(closeStopCh bool) error {
if err := d.file.Sync(); err != nil {
return err
Expand Down Expand Up @@ -145,10 +151,8 @@ func (d *Writer) Write(p []byte) (n int, err error) {
return
}

// update written size
// update size and rotate file
d.written += uint64(n)

// rotate file
err = d.doRotate()
return
}
Expand All @@ -171,8 +175,10 @@ func (d *Writer) doRotate() (err error) {
err = d.rotatingByTime()
}

// async clean backup files. TODO only call on file rotated.
d.asyncClean()
// async clean backup files.
if d.shouldClean(true) {
d.asyncClean()
}
return
}

Expand Down Expand Up @@ -243,27 +249,26 @@ func (d *Writer) rotatingFile(bakFile string, rename bool) error {
return nil
}

// open the log file. and set the d.file, d.path
func (d *Writer) openFile(logfile string) error {
file, err := fsutil.OpenFile(logfile, DefaultFileFlags, d.cfg.FilePerm)
if err != nil {
return err
}

d.path = logfile
d.file = file
return nil
}

//
// ---------------------------------------------------------------------------
// clean backup files
// ---------------------------------------------------------------------------
//

// check should clean old files by config
func (d *Writer) shouldClean(withRand bool) bool {
cfgIsYes := d.cfg.BackupNum > 0 || d.cfg.BackupTime > 0
if !withRand {
return cfgIsYes
}

// 20% probability trigger clean
return cfgIsYes && rand.Intn(100) < 20
}

// async clean old files by config. should be in lock.
func (d *Writer) asyncClean() {
if d.cfg.BackupNum == 0 && d.cfg.BackupTime == 0 {
if !d.shouldClean(false) {
return
}

Expand Down Expand Up @@ -386,6 +391,24 @@ func (d *Writer) Clean() (err error) {
return
}

//
// ---------------------------------------------------------------------------
// helper methods
// ---------------------------------------------------------------------------
//

// open the log file. and set the d.file, d.path
func (d *Writer) openFile(logfile string) error {
file, err := fsutil.OpenFile(logfile, DefaultFileFlags, d.cfg.FilePerm)
if err != nil {
return err
}

d.path = logfile
d.file = file
return nil
}

func (d *Writer) buildFilterFns(fileName string) []fsutil.FilterFunc {
filterFns := []fsutil.FilterFunc{
fsutil.OnlyFindFile,
Expand Down

0 comments on commit e492570

Please sign in to comment.