Skip to content

Commit

Permalink
Merge pull request #1783 from Isaacedvr1003/main
Browse files Browse the repository at this point in the history
gva项目新增对logs下文件夹的定期清理功能,也可配置不清理,较原框架有改进。
  • Loading branch information
pixelmaxQm committed Jun 15, 2024
2 parents d840841 + 4436e2e commit 5761d21
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
1 change: 1 addition & 0 deletions server/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ zap:
encode-level: LowercaseColorLevelEncoder
stacktrace-key: stacktrace
log-in-console: true
retention-day: -1

# redis configuration
redis:
Expand Down
1 change: 1 addition & 0 deletions server/config/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Zap struct {
StacktraceKey string `mapstructure:"stacktrace-key" json:"stacktrace-key" yaml:"stacktrace-key"` // 栈名
ShowLine bool `mapstructure:"show-line" json:"show-line" yaml:"show-line"` // 显示行
LogInConsole bool `mapstructure:"log-in-console" json:"log-in-console" yaml:"log-in-console"` // 输出控制台
RetentionDay int `mapstructure:"retention-day" json:"retention-day" yaml:"retention-day"` // 日志保留天数
}

// Levels 根据字符串转化为 zapcore.Levels
Expand Down
46 changes: 36 additions & 10 deletions server/core/internal/cutter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
// Cutter 实现 io.Writer 接口
// 用于日志切割, strings.Join([]string{director,layout, formats..., level+".log"}, os.PathSeparator)
type Cutter struct {
level string // 日志级别(debug, info, warn, error, dpanic, panic, fatal)
layout string // 时间格式 2006-01-02 15:04:05
formats []string // 自定义参数([]string{Director,"2006-01-02", "business"(此参数可不写), level+".log"}
director string // 日志文件夹
file *os.File // 文件句柄
mutex *sync.RWMutex // 读写锁
level string // 日志级别(debug, info, warn, error, dpanic, panic, fatal)
layout string // 时间格式 2006-01-02 15:04:05
formats []string // 自定义参数([]string{Director,"2006-01-02", "business"(此参数可不写), level+".log"}
director string // 日志文件夹
retentionDay int //日志保留天数
file *os.File // 文件句柄
mutex *sync.RWMutex // 读写锁
}

type CutterOption func(*Cutter)
Expand All @@ -36,11 +37,12 @@ func CutterWithFormats(format ...string) CutterOption {
}
}

func NewCutter(director string, level string, options ...CutterOption) *Cutter {
func NewCutter(director string, level string, retentionDay int, options ...CutterOption) *Cutter {
rotate := &Cutter{
level: level,
director: director,
mutex: new(sync.RWMutex),
level: level,
director: director,
retentionDay: retentionDay,
mutex: new(sync.RWMutex),
}
for i := 0; i < len(options); i++ {
options[i](rotate)
Expand Down Expand Up @@ -77,6 +79,10 @@ func (c *Cutter) Write(bytes []byte) (n int, err error) {
if err != nil {
return 0, err
}
err = removeNDaysFolders(c.director, c.retentionDay)
if err != nil {
return 0, err
}
c.file, err = os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return 0, err
Expand All @@ -93,3 +99,23 @@ func (c *Cutter) Sync() error {
}
return nil
}

// 增加日志目录文件清理 小于等于零的值默认忽略不再处理
func removeNDaysFolders(dir string, days int) error {
if days <= 0 {
return nil
}
cutoff := time.Now().AddDate(0, 0, -days)
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && info.ModTime().Before(cutoff) && path != dir {
err = os.RemoveAll(path)
if err != nil {
return err
}
}
return nil
})
}
1 change: 1 addition & 0 deletions server/core/internal/zap_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (z *ZapCore) WriteSyncer(formats ...string) zapcore.WriteSyncer {
cutter := NewCutter(
global.GVA_CONFIG.Zap.Director,
z.level.String(),
global.GVA_CONFIG.Zap.RetentionDay,
CutterWithLayout(time.DateOnly),
CutterWithFormats(formats...),
)
Expand Down

0 comments on commit 5761d21

Please sign in to comment.