diff --git a/config.go b/config.go index 2c34459..4b223b9 100644 --- a/config.go +++ b/config.go @@ -9,7 +9,9 @@ package logger import ( "fmt" + "git.zhangdeman.cn/zhangdeman/consts" "os" + "path/filepath" "strings" "time" @@ -19,19 +21,6 @@ import ( // TimeIntervalType 日志时间间隔类型 type TimeIntervalType uint -const ( - // TimeIntervalTypeMinute 按分钟切割 - TimeIntervalTypeMinute = TimeIntervalType(0) - // TimeIntervalTypeHour 按小时切割 - TimeIntervalTypeHour = TimeIntervalType(1) - // TimeIntervalTypeDay 按天切割 - TimeIntervalTypeDay = TimeIntervalType(2) - // TimeIntervalTypeMonth 按月切割 - TimeIntervalTypeMonth = TimeIntervalType(3) - // TimeIntervalTypeYear 按年切割 - TimeIntervalTypeYear = TimeIntervalType(4) -) - const ( // DefaultDivisionChar 默认的时间格式分隔符 DefaultDivisionChar = "-" @@ -43,13 +32,13 @@ const ( // // Date : 3:08 下午 2021/1/2 type RotateLogConfig struct { - TimeIntervalType TimeIntervalType `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 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"` // 日志最长保存时间 } // SetRotateLogConfigFunc 设置日志切割的选项 @@ -64,7 +53,7 @@ type SetRotateLogConfigFunc func(rlc *RotateLogConfig) // Author : go_developer@163.com<白茶清欢> // // Date : 3:34 下午 2021/1/2 -func WithTimeIntervalType(timeIntervalType TimeIntervalType) SetRotateLogConfigFunc { +func WithTimeIntervalType(timeIntervalType string) SetRotateLogConfigFunc { return func(rlc *RotateLogConfig) { rlc.TimeIntervalType = timeIntervalType } @@ -102,7 +91,7 @@ func NewRotateLogConfig(logPath string, logFile string, option ...SetRotateLogCo return nil, LogPathEmptyError() } c := &RotateLogConfig{ - TimeIntervalType: TimeIntervalTypeHour, + TimeIntervalType: consts.LogSplitHour, LogPath: logPath, LogFileName: logFile, DivisionChar: "", @@ -130,36 +119,31 @@ func formatConfig(c *RotateLogConfig) error { c.DivisionChar = DefaultDivisionChar } // 格式化路径 - logPathByte := []byte(c.LogPath) - if string(logPathByte[len(logPathByte)-1]) != "/" { - c.LogPath = c.LogPath + "/" - } + c.LogPath = strings.TrimRight(c.LogPath, string(filepath.Separator)) + string(filepath.Separator) + // 检测路径是否存在,不存在自动创建 if _, err := os.Stat(c.LogPath); nil != err { if !os.IsNotExist(err) { // 异常不是路径不存在,抛异常 return DealLogPathError(err, c.LogPath) } - if err := os.Mkdir(c.LogPath, os.ModePerm); nil != err { + if err = os.Mkdir(c.LogPath, os.ModePerm); nil != err { return DealLogPathError(err, "创建日志目录失败") } } // 生成格式化日志全路径 - switch c.TimeIntervalType { - case TimeIntervalTypeMinute: - c.TimeInterval = time.Minute - c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + "%m" + c.DivisionChar + "%d" + c.DivisionChar + "%H" + c.DivisionChar + "%M" + c.DivisionChar + c.LogFileName - case TimeIntervalTypeHour: + switch strings.ToUpper(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 - case TimeIntervalTypeDay: + case consts.LogSplitDay: c.TimeInterval = time.Hour * 24 c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + "%m" + c.DivisionChar + "%d" + c.DivisionChar + c.LogFileName - case TimeIntervalTypeMonth: + case consts.LogSplitMonth: c.TimeInterval = time.Hour * 24 * 30 c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + "%m" + c.DivisionChar + c.LogFileName - case TimeIntervalTypeYear: + case consts.LogSplitYear: c.TimeInterval = time.Hour * 24 * 365 c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + c.LogFileName default: diff --git a/error.go b/error.go index 1a5ddf4..c0b359f 100644 --- a/error.go +++ b/error.go @@ -50,7 +50,7 @@ func DealLogPathError(err error, logPath string) error { // Author : go_developer@163.com<白茶清欢> // // Date : 4:50 下午 2021/1/2 -func LogSplitTypeError(splitType TimeIntervalType) error { +func LogSplitTypeError(splitType string) error { return errors.Wrapf(errors.New("日志切割时间类型错误"), "日志切割时间类型错误, 传入类型 : %v", splitType) } diff --git a/logger.go b/logger.go index 13b54b6..6fc2ef0 100644 --- a/logger.go +++ b/logger.go @@ -8,8 +8,10 @@ package logger import ( + "git.zhangdeman.cn/zhangdeman/consts" "io" "os" + "strings" "github.com/pkg/errors" @@ -25,11 +27,26 @@ import ( // Author : go_developer@163.com<白茶清欢> // // Date : 5:05 下午 2021/1/2 -func NewLogger(loggerLevel zapcore.Level, splitConfig *RotateLogConfig, optionFunc ...SetLoggerOptionFunc) (*zap.Logger, error) { +func NewLogger(inputLoggerLevel string, splitConfig *RotateLogConfig, optionFunc ...SetLoggerOptionFunc) (*zap.Logger, error) { if nil == splitConfig { return nil, errors.New("未配置日志切割规则") } + loggerLevel := zapcore.DebugLevel + switch strings.ToUpper(inputLoggerLevel) { + case consts.LogLevelDebug: + loggerLevel = zapcore.DebugLevel + case consts.LogLevelInfo: + loggerLevel = zapcore.InfoLevel + case consts.LogLevelWarn: + loggerLevel = zapcore.WarnLevel + case consts.LogLevelError: + loggerLevel = zapcore.ErrorLevel + case consts.LogLevelPanic: + loggerLevel = zapcore.PanicLevel + default: + return nil, errors.New(inputLoggerLevel + " : input log level is not support") + } o := &OptionLogger{} for _, f := range optionFunc {