日志升级相关枚举值定义

This commit is contained in:
2024-11-25 16:33:16 +08:00
parent 98eb52ae30
commit eab7bab9d7
7 changed files with 114 additions and 66 deletions

View File

@ -34,13 +34,13 @@ const (
//
// Date : 3:08 下午 2021/1/2
type RotateLogConfig struct {
TimeIntervalType string `json:"time_interval_type" yaml:"time_interval_type"` // 日志切割的时间间隔类型 0 - 小时 1 - 天 2 - 月 3 - 年
TimeInterval time.Duration `json:"time_interval" yaml:"time_interval"` // 日志切割的时间间隔
LogPath string `json:"log_path" yaml:"log_path"` // 存储日志的路径
LogFileName string `json:"log_file_name" yaml:"log_file_name"` // 日志文件名
DivisionChar string `json:"division_char" yaml:"division_char"` // 日志文件拼时间分隔符
FullLogFormat string `json:"full_log_format" yaml:"full_log_format"` // 完整的日志格式
MaxAge time.Duration `json:"max_age" yaml:"max_age"` // 日志最长保存时间
TimeIntervalType consts.LogSplit `json:"time_interval_type" yaml:"time_interval_type"` // 日志切割的时间间隔类型 0 - 小时 1 - 天 2 - 月 3 - 年
TimeInterval time.Duration `json:"time_interval" yaml:"time_interval"` // 日志切割的时间间隔
LogPath string `json:"log_path" yaml:"log_path"` // 存储日志的路径
LogFileName string `json:"log_file_name" yaml:"log_file_name"` // 日志文件名
DivisionChar string `json:"division_char" yaml:"division_char"` // 日志文件拼时间分隔符
FullLogFormat string `json:"full_log_format" yaml:"full_log_format"` // 完整的日志格式
MaxAge time.Duration `json:"max_age" yaml:"max_age"` // 日志最长保存时间
}
// SetRotateLogConfigFunc 设置日志切割的选项
@ -55,7 +55,7 @@ type SetRotateLogConfigFunc func(rlc *RotateLogConfig)
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:34 下午 2021/1/2
func WithTimeIntervalType(timeIntervalType string) SetRotateLogConfigFunc {
func WithTimeIntervalType(timeIntervalType consts.LogSplit) SetRotateLogConfigFunc {
return func(rlc *RotateLogConfig) {
rlc.TimeIntervalType = timeIntervalType
}
@ -134,8 +134,13 @@ func formatConfig(c *RotateLogConfig) error {
}
}
c.TimeIntervalType = consts.LogSplit(strings.ToUpper(c.TimeIntervalType.String()))
if !c.TimeIntervalType.IsValid() {
// 非法的日志切割规则,默认按天切
c.TimeIntervalType = consts.LogSplitDay
}
// 生成格式化日志全路径
switch strings.ToUpper(c.TimeIntervalType) {
switch c.TimeIntervalType {
case consts.LogSplitHour:
c.TimeInterval = time.Hour
c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + "%m" + c.DivisionChar + "%d" + c.DivisionChar + "%H" + c.DivisionChar + c.LogFileName
@ -149,7 +154,7 @@ func formatConfig(c *RotateLogConfig) error {
c.TimeInterval = time.Hour * 24 * 365
c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + c.LogFileName
default:
return LogSplitTypeError(c.TimeIntervalType)
return LogSplitTypeError(c.TimeIntervalType.String())
}
return nil