|
|
@ -2,6 +2,7 @@ package logx
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
@ -13,18 +14,58 @@ import (
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func TestDailyRotateRuleMarkRotated(t *testing.T) {
|
|
|
|
func TestDailyRotateRuleMarkRotated(t *testing.T) {
|
|
|
|
|
|
|
|
t.Run("daily rule", func(t *testing.T) {
|
|
|
|
var rule DailyRotateRule
|
|
|
|
var rule DailyRotateRule
|
|
|
|
rule.MarkRotated()
|
|
|
|
rule.MarkRotated()
|
|
|
|
assert.Equal(t, getNowDate(), rule.rotatedTime)
|
|
|
|
assert.Equal(t, getNowDate(), rule.rotatedTime)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("daily rule", func(t *testing.T) {
|
|
|
|
|
|
|
|
rule := DefaultRotateRule("test", "-", 1, false)
|
|
|
|
|
|
|
|
_, ok := rule.(*DailyRotateRule)
|
|
|
|
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestDailyRotateRuleOutdatedFiles(t *testing.T) {
|
|
|
|
func TestDailyRotateRuleOutdatedFiles(t *testing.T) {
|
|
|
|
|
|
|
|
t.Run("no files", func(t *testing.T) {
|
|
|
|
var rule DailyRotateRule
|
|
|
|
var rule DailyRotateRule
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
rule.days = 1
|
|
|
|
rule.days = 1
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
rule.gzip = true
|
|
|
|
rule.gzip = true
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("bad files", func(t *testing.T) {
|
|
|
|
|
|
|
|
rule := DailyRotateRule{
|
|
|
|
|
|
|
|
filename: "[a-z",
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
|
|
|
|
rule.days = 1
|
|
|
|
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
|
|
|
|
rule.gzip = true
|
|
|
|
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("temp files", func(t *testing.T) {
|
|
|
|
|
|
|
|
boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay) * 2).Format(dateFormat)
|
|
|
|
|
|
|
|
f1, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_ = f1.Close()
|
|
|
|
|
|
|
|
f2, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_ = f2.Close()
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
|
|
|
_ = os.Remove(f1.Name())
|
|
|
|
|
|
|
|
_ = os.Remove(f2.Name())
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
rule := DailyRotateRule{
|
|
|
|
|
|
|
|
filename: path.Join(os.TempDir(), "go-zero-test-"),
|
|
|
|
|
|
|
|
days: 1,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NotEmpty(t, rule.OutdatedFiles())
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestDailyRotateRuleShallRotate(t *testing.T) {
|
|
|
|
func TestDailyRotateRuleShallRotate(t *testing.T) {
|
|
|
@ -34,12 +75,21 @@ func TestDailyRotateRuleShallRotate(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestSizeLimitRotateRuleMarkRotated(t *testing.T) {
|
|
|
|
func TestSizeLimitRotateRuleMarkRotated(t *testing.T) {
|
|
|
|
|
|
|
|
t.Run("size limit rule", func(t *testing.T) {
|
|
|
|
var rule SizeLimitRotateRule
|
|
|
|
var rule SizeLimitRotateRule
|
|
|
|
rule.MarkRotated()
|
|
|
|
rule.MarkRotated()
|
|
|
|
assert.Equal(t, getNowDateInRFC3339Format(), rule.rotatedTime)
|
|
|
|
assert.Equal(t, getNowDateInRFC3339Format(), rule.rotatedTime)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("size limit rule", func(t *testing.T) {
|
|
|
|
|
|
|
|
rule := NewSizeLimitRotateRule("foo", "-", 1, 1, 1, false)
|
|
|
|
|
|
|
|
rule.MarkRotated()
|
|
|
|
|
|
|
|
assert.Equal(t, getNowDateInRFC3339Format(), rule.(*SizeLimitRotateRule).rotatedTime)
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestSizeLimitRotateRuleOutdatedFiles(t *testing.T) {
|
|
|
|
func TestSizeLimitRotateRuleOutdatedFiles(t *testing.T) {
|
|
|
|
|
|
|
|
t.Run("no files", func(t *testing.T) {
|
|
|
|
var rule SizeLimitRotateRule
|
|
|
|
var rule SizeLimitRotateRule
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
rule.days = 1
|
|
|
|
rule.days = 1
|
|
|
@ -48,6 +98,78 @@ func TestSizeLimitRotateRuleOutdatedFiles(t *testing.T) {
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
rule.maxBackups = 0
|
|
|
|
rule.maxBackups = 0
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("bad files", func(t *testing.T) {
|
|
|
|
|
|
|
|
rule := SizeLimitRotateRule{
|
|
|
|
|
|
|
|
DailyRotateRule: DailyRotateRule{
|
|
|
|
|
|
|
|
filename: "[a-z",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
|
|
|
|
rule.days = 1
|
|
|
|
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
|
|
|
|
rule.gzip = true
|
|
|
|
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("temp files", func(t *testing.T) {
|
|
|
|
|
|
|
|
boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay) * 2).Format(dateFormat)
|
|
|
|
|
|
|
|
f1, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
f2, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
boundary1 := time.Now().Add(time.Hour * time.Duration(hoursPerDay) * 2).Format(dateFormat)
|
|
|
|
|
|
|
|
f3, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary1)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
|
|
|
_ = f1.Close()
|
|
|
|
|
|
|
|
_ = os.Remove(f1.Name())
|
|
|
|
|
|
|
|
_ = f2.Close()
|
|
|
|
|
|
|
|
_ = os.Remove(f2.Name())
|
|
|
|
|
|
|
|
_ = f3.Close()
|
|
|
|
|
|
|
|
_ = os.Remove(f3.Name())
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
rule := SizeLimitRotateRule{
|
|
|
|
|
|
|
|
DailyRotateRule: DailyRotateRule{
|
|
|
|
|
|
|
|
filename: path.Join(os.TempDir(), "go-zero-test-"),
|
|
|
|
|
|
|
|
days: 1,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
maxBackups: 3,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NotEmpty(t, rule.OutdatedFiles())
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("no backups", func(t *testing.T) {
|
|
|
|
|
|
|
|
boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay) * 2).Format(dateFormat)
|
|
|
|
|
|
|
|
f1, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
f2, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
boundary1 := time.Now().Add(time.Hour * time.Duration(hoursPerDay) * 2).Format(dateFormat)
|
|
|
|
|
|
|
|
f3, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary1)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
|
|
|
_ = f1.Close()
|
|
|
|
|
|
|
|
_ = os.Remove(f1.Name())
|
|
|
|
|
|
|
|
_ = f2.Close()
|
|
|
|
|
|
|
|
_ = os.Remove(f2.Name())
|
|
|
|
|
|
|
|
_ = f3.Close()
|
|
|
|
|
|
|
|
_ = os.Remove(f3.Name())
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
rule := SizeLimitRotateRule{
|
|
|
|
|
|
|
|
DailyRotateRule: DailyRotateRule{
|
|
|
|
|
|
|
|
filename: path.Join(os.TempDir(), "go-zero-test-"),
|
|
|
|
|
|
|
|
days: 1,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NotEmpty(t, rule.OutdatedFiles())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger := new(RotateLogger)
|
|
|
|
|
|
|
|
logger.rule = &rule
|
|
|
|
|
|
|
|
logger.maybeDeleteOutdatedFiles()
|
|
|
|
|
|
|
|
assert.Empty(t, rule.OutdatedFiles())
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestSizeLimitRotateRuleShallRotate(t *testing.T) {
|
|
|
|
func TestSizeLimitRotateRuleShallRotate(t *testing.T) {
|
|
|
@ -61,6 +183,7 @@ func TestSizeLimitRotateRuleShallRotate(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestRotateLoggerClose(t *testing.T) {
|
|
|
|
func TestRotateLoggerClose(t *testing.T) {
|
|
|
|
|
|
|
|
t.Run("close", func(t *testing.T) {
|
|
|
|
filename, err := fs.TempFilenameWithText("foo")
|
|
|
|
filename, err := fs.TempFilenameWithText("foo")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
if len(filename) > 0 {
|
|
|
|
if len(filename) > 0 {
|
|
|
@ -68,7 +191,18 @@ func TestRotateLoggerClose(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger, err := NewLogger(filename, new(DailyRotateRule), false)
|
|
|
|
logger, err := NewLogger(filename, new(DailyRotateRule), false)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
_, err = logger.Write([]byte("foo"))
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, logger.Close())
|
|
|
|
assert.Nil(t, logger.Close())
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("close and write", func(t *testing.T) {
|
|
|
|
|
|
|
|
logger := new(RotateLogger)
|
|
|
|
|
|
|
|
logger.done = make(chan struct{})
|
|
|
|
|
|
|
|
close(logger.done)
|
|
|
|
|
|
|
|
_, err := logger.Write([]byte("foo"))
|
|
|
|
|
|
|
|
assert.ErrorIs(t, err, ErrLogFileClosed)
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestRotateLoggerGetBackupFilename(t *testing.T) {
|
|
|
|
func TestRotateLoggerGetBackupFilename(t *testing.T) {
|
|
|
|