diff --git a/loggerx.go b/loggerx.go index 8455fd0..8fa57db 100644 --- a/loggerx.go +++ b/loggerx.go @@ -21,6 +21,7 @@ import ( // 需要实现io.Writer接口 type Logger struct { + ctx context.Context filePath *sync.Map // filePath mu *sync.Mutex option loggerOption @@ -44,6 +45,7 @@ func NewLogger(ctx context.Context, opts ...Option) *Logger { } l := &Logger{ + ctx: ctx, filePath: &sync.Map{}, mu: &sync.Mutex{}, option: opt, diff --git a/remove.go b/remove.go index ca0a4fe..9fd5b60 100644 --- a/remove.go +++ b/remove.go @@ -10,6 +10,23 @@ import ( // 监听dir文件夹的所有文件,循环查找是否有超过days天的文件,删除掉 func (l *Logger) delete() { + + tick := time.NewTicker(time.Hour) + + for { + select { + case <-tick.C: + err := l.walkAndDel() + if err != nil { + fmt.Println(err) + } + case <-l.ctx.Done(): + return + } + } +} + +func (l *Logger) walkAndDel() error { err := filepath.Walk(l.option.dir, func(path string, info os.FileInfo, err error) error { if err != nil { fmt.Println(err) @@ -38,15 +55,10 @@ func (l *Logger) delete() { return os.Remove(path) }) - if err != nil { - fmt.Println(err) - } - - // 监听文件夹,获取新加入的文件 - - + return err } +// 判断空文件夹 func isEmptyDir(path string) bool { files, err := os.ReadDir(path) if err != nil {