添加自动文件压缩的实现

This commit is contained in:
yun
2026-09-13 21:53:37 +08:00
parent ab11fa71a0
commit 316c6420a7
21 changed files with 2037 additions and 66 deletions
+55
View File
@@ -0,0 +1,55 @@
package loggerx_test
import (
"context"
"fmt"
"strings"
"sync"
"testing"
"github.com/yuninks/loggerx"
)
// MustSync 与写入并发时不能丢日志(模拟外部定时刷盘的用法)
func TestMustSyncConcurrentNoLoss(t *testing.T) {
for round := 0; round < 5; round++ {
dir := t.TempDir()
l := loggerx.NewLogger(context.Background(), loggerx.SetDir(dir))
ctx := context.Background()
const n = 2000
var wg sync.WaitGroup
wg.Add(2)
// 持续写入
go func() {
defer wg.Done()
for i := 0; i < n; i++ {
l.Infof(ctx, "MS-%d", i)
}
}()
// 持续外部刷盘(这正是 MustSync 的公开用途)
go func() {
defer wg.Done()
for i := 0; i < 300; i++ {
_ = l.MustSync()
}
}()
wg.Wait()
if err := l.Close(); err != nil {
t.Fatalf("第 %d 轮 Close: %v", round, err)
}
content := readAllLogs(t, dir)
missing := 0
for i := 0; i < n; i++ {
if !strings.Contains(content, fmt.Sprintf(`MS-%d"`, i)) {
missing++
}
}
if missing > 0 {
t.Errorf("第 %d 轮:写入 %d 条,丢失 %d 条", round, n, missing)
}
}
}