过期删除文件
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
|||||||
|
|
||||||
// 需要实现io.Writer接口
|
// 需要实现io.Writer接口
|
||||||
type Logger struct {
|
type Logger struct {
|
||||||
|
ctx context.Context
|
||||||
filePath *sync.Map // filePath
|
filePath *sync.Map // filePath
|
||||||
mu *sync.Mutex
|
mu *sync.Mutex
|
||||||
option loggerOption
|
option loggerOption
|
||||||
@@ -44,6 +45,7 @@ func NewLogger(ctx context.Context, opts ...Option) *Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
l := &Logger{
|
l := &Logger{
|
||||||
|
ctx: ctx,
|
||||||
filePath: &sync.Map{},
|
filePath: &sync.Map{},
|
||||||
mu: &sync.Mutex{},
|
mu: &sync.Mutex{},
|
||||||
option: opt,
|
option: opt,
|
||||||
|
|||||||
@@ -10,6 +10,23 @@ import (
|
|||||||
// 监听dir文件夹的所有文件,循环查找是否有超过days天的文件,删除掉
|
// 监听dir文件夹的所有文件,循环查找是否有超过days天的文件,删除掉
|
||||||
|
|
||||||
func (l *Logger) delete() {
|
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 {
|
err := filepath.Walk(l.option.dir, func(path string, info os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@@ -38,15 +55,10 @@ func (l *Logger) delete() {
|
|||||||
return os.Remove(path)
|
return os.Remove(path)
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
return err
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 监听文件夹,获取新加入的文件
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 判断空文件夹
|
||||||
func isEmptyDir(path string) bool {
|
func isEmptyDir(path string) bool {
|
||||||
files, err := os.ReadDir(path)
|
files, err := os.ReadDir(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user