Skip to content

Commit

Permalink
✅ test: update the file daemon clean tests, update README docs
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 9, 2023
1 parent 43c24a5 commit 456568f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
10 changes: 6 additions & 4 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@
- 支持按时间、按大小自动分割文件
- 支持配置通过 `gzip` 压缩日志文件
- 支持清理旧日志文件 配置: `BackupNum` `BackupTime`
- `rotatefile` 包也可以用在其他日志库。例如:`log``glog``zap` 等等。

> NEW: `v0.3.0` 废弃原来实现的纷乱的各种handler,统一抽象为
> `FlushCloseHandler` `SyncCloseHandler` `WriteCloserHandler` `IOWriterHandler`
> 几个支持不同类型writer的处理器。让构建自定义 Handler 更加简单,内置的handlers也基本上由它们组成。
### `rotatefile` 子包

- `rotatefile` 子包是一个拥有文件分割,清理,压缩备份的独立工具库
- `rotatefile.Writer` 也可以直接包装使用用在其他日志库。例如:`log``glog``zap` 等等
- `rotatefile.FilesClear` 是一个独立的文件清理备份工具, 可以用在其他地方(如 PHP等其他程序日志清理)
- 更多使用请查看 [rotatefile](rotatefile/README.md)

## [English](README.md)

Expand Down
39 changes: 38 additions & 1 deletion rotatefile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ type Config struct {
})

// clear files on daemon
go fc.DaemonClean()
go fc.DaemonClean(nil)

// NOTE: stop daemon before exit
// fc.QuitDaemon()
Expand All @@ -128,4 +128,41 @@ type Config struct {

```go

// CConfig struct for clean files
type CConfig struct {
// BackupNum max number for keep old files.
// 0 is not limit, default is 20.
BackupNum uint `json:"backup_num" yaml:"backup_num"`

// BackupTime max time for keep old files, unit is TimeUnit.
//
// 0 is not limit, default is a week.
BackupTime uint `json:"backup_time" yaml:"backup_time"`

// 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"`

// Patterns dir path with filename match patterns.
//
// eg: ["/tmp/error.log.*", "/path/to/info.log.*", "/path/to/dir/*"]
Patterns []string `json:"patterns" yaml:"patterns"`

// TimeClock for clean files
TimeClock Clocker

// TimeUnit for BackupTime. default is hours: time.Hour
TimeUnit time.Duration `json:"time_unit" yaml:"time_unit"`

// CheckInterval for clean files on daemon run. default is 60s.
CheckInterval time.Duration `json:"check_interval" yaml:"check_interval"`

// IgnoreError ignore remove error
// TODO IgnoreError bool

// RotateMode for rotate split files TODO
// - copy+cut: copy contents then truncate file
// - rename : rename file(use for like PHP-FPM app)
// RotateMode RotateMode `json:"rotate_mode" yaml:"rotate_mode"`
}
```
7 changes: 4 additions & 3 deletions rotatefile/cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/goutil/timex"
"github.com/gookit/slog/rotatefile"
)

Expand Down Expand Up @@ -87,14 +88,14 @@ func TestFilesClear_DaemonClean(t *testing.T) {

// start daemon
go fc.DaemonClean(func() {
fmt.Println("daemon clean stopped")
fmt.Println("daemon clean stopped, at", timex.Now().DateFormat("ymdTH:i:s.v"))
wg.Done()
})

// stop daemon
go func() {
time.Sleep(time.Second * 1)
fmt.Println("stop daemon clean")
time.Sleep(time.Millisecond * 1200)
fmt.Println("stop daemon clean, at", timex.Now().DateFormat("ymdTH:i:s.v"))
fc.StopDaemon()
}()

Expand Down

0 comments on commit 456568f

Please sign in to comment.