升级log_level处理
This commit is contained in:
parent
16d33f84ca
commit
408f33b21e
54
config.go
54
config.go
@ -9,7 +9,9 @@ package logger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/consts"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -19,19 +21,6 @@ import (
|
|||||||
// TimeIntervalType 日志时间间隔类型
|
// TimeIntervalType 日志时间间隔类型
|
||||||
type TimeIntervalType uint
|
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 (
|
const (
|
||||||
// DefaultDivisionChar 默认的时间格式分隔符
|
// DefaultDivisionChar 默认的时间格式分隔符
|
||||||
DefaultDivisionChar = "-"
|
DefaultDivisionChar = "-"
|
||||||
@ -43,13 +32,13 @@ const (
|
|||||||
//
|
//
|
||||||
// Date : 3:08 下午 2021/1/2
|
// Date : 3:08 下午 2021/1/2
|
||||||
type RotateLogConfig struct {
|
type RotateLogConfig struct {
|
||||||
TimeIntervalType TimeIntervalType `json:"time_interval_type" yaml:"time_interval_type"` // 日志切割的时间间隔类型 0 - 小时 1 - 天 2 - 月 3 - 年
|
TimeIntervalType string `json:"time_interval_type" yaml:"time_interval_type"` // 日志切割的时间间隔类型 0 - 小时 1 - 天 2 - 月 3 - 年
|
||||||
TimeInterval time.Duration `json:"time_interval" yaml:"time_interval"` // 日志切割的时间间隔
|
TimeInterval time.Duration `json:"time_interval" yaml:"time_interval"` // 日志切割的时间间隔
|
||||||
LogPath string `json:"log_path" yaml:"log_path"` // 存储日志的路径
|
LogPath string `json:"log_path" yaml:"log_path"` // 存储日志的路径
|
||||||
LogFileName string `json:"log_file_name" yaml:"log_file_name"` // 日志文件名
|
LogFileName string `json:"log_file_name" yaml:"log_file_name"` // 日志文件名
|
||||||
DivisionChar string `json:"division_char" yaml:"division_char"` // 日志文件拼时间分隔符
|
DivisionChar string `json:"division_char" yaml:"division_char"` // 日志文件拼时间分隔符
|
||||||
FullLogFormat string `json:"full_log_format" yaml:"full_log_format"` // 完整的日志格式
|
FullLogFormat string `json:"full_log_format" yaml:"full_log_format"` // 完整的日志格式
|
||||||
MaxAge time.Duration `json:"max_age" yaml:"max_age"` // 日志最长保存时间
|
MaxAge time.Duration `json:"max_age" yaml:"max_age"` // 日志最长保存时间
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetRotateLogConfigFunc 设置日志切割的选项
|
// SetRotateLogConfigFunc 设置日志切割的选项
|
||||||
@ -64,7 +53,7 @@ type SetRotateLogConfigFunc func(rlc *RotateLogConfig)
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 3:34 下午 2021/1/2
|
// Date : 3:34 下午 2021/1/2
|
||||||
func WithTimeIntervalType(timeIntervalType TimeIntervalType) SetRotateLogConfigFunc {
|
func WithTimeIntervalType(timeIntervalType string) SetRotateLogConfigFunc {
|
||||||
return func(rlc *RotateLogConfig) {
|
return func(rlc *RotateLogConfig) {
|
||||||
rlc.TimeIntervalType = timeIntervalType
|
rlc.TimeIntervalType = timeIntervalType
|
||||||
}
|
}
|
||||||
@ -102,7 +91,7 @@ func NewRotateLogConfig(logPath string, logFile string, option ...SetRotateLogCo
|
|||||||
return nil, LogPathEmptyError()
|
return nil, LogPathEmptyError()
|
||||||
}
|
}
|
||||||
c := &RotateLogConfig{
|
c := &RotateLogConfig{
|
||||||
TimeIntervalType: TimeIntervalTypeHour,
|
TimeIntervalType: consts.LogSplitHour,
|
||||||
LogPath: logPath,
|
LogPath: logPath,
|
||||||
LogFileName: logFile,
|
LogFileName: logFile,
|
||||||
DivisionChar: "",
|
DivisionChar: "",
|
||||||
@ -130,36 +119,31 @@ func formatConfig(c *RotateLogConfig) error {
|
|||||||
c.DivisionChar = DefaultDivisionChar
|
c.DivisionChar = DefaultDivisionChar
|
||||||
}
|
}
|
||||||
// 格式化路径
|
// 格式化路径
|
||||||
logPathByte := []byte(c.LogPath)
|
c.LogPath = strings.TrimRight(c.LogPath, string(filepath.Separator)) + string(filepath.Separator)
|
||||||
if string(logPathByte[len(logPathByte)-1]) != "/" {
|
|
||||||
c.LogPath = c.LogPath + "/"
|
|
||||||
}
|
|
||||||
// 检测路径是否存在,不存在自动创建
|
// 检测路径是否存在,不存在自动创建
|
||||||
if _, err := os.Stat(c.LogPath); nil != err {
|
if _, err := os.Stat(c.LogPath); nil != err {
|
||||||
if !os.IsNotExist(err) {
|
if !os.IsNotExist(err) {
|
||||||
// 异常不是路径不存在,抛异常
|
// 异常不是路径不存在,抛异常
|
||||||
return DealLogPathError(err, c.LogPath)
|
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, "创建日志目录失败")
|
return DealLogPathError(err, "创建日志目录失败")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成格式化日志全路径
|
// 生成格式化日志全路径
|
||||||
switch c.TimeIntervalType {
|
switch strings.ToUpper(c.TimeIntervalType) {
|
||||||
case TimeIntervalTypeMinute:
|
case consts.LogSplitHour:
|
||||||
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:
|
|
||||||
c.TimeInterval = time.Hour
|
c.TimeInterval = time.Hour
|
||||||
c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + "%m" + c.DivisionChar + "%d" + c.DivisionChar + "%H" + c.DivisionChar + c.LogFileName
|
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.TimeInterval = time.Hour * 24
|
||||||
c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + "%m" + c.DivisionChar + "%d" + c.DivisionChar + c.LogFileName
|
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.TimeInterval = time.Hour * 24 * 30
|
||||||
c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + "%m" + c.DivisionChar + c.LogFileName
|
c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + "%m" + c.DivisionChar + c.LogFileName
|
||||||
case TimeIntervalTypeYear:
|
case consts.LogSplitYear:
|
||||||
c.TimeInterval = time.Hour * 24 * 365
|
c.TimeInterval = time.Hour * 24 * 365
|
||||||
c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + c.LogFileName
|
c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + c.LogFileName
|
||||||
default:
|
default:
|
||||||
|
2
error.go
2
error.go
@ -50,7 +50,7 @@ func DealLogPathError(err error, logPath string) error {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 4:50 下午 2021/1/2
|
// Date : 4:50 下午 2021/1/2
|
||||||
func LogSplitTypeError(splitType TimeIntervalType) error {
|
func LogSplitTypeError(splitType string) error {
|
||||||
return errors.Wrapf(errors.New("日志切割时间类型错误"), "日志切割时间类型错误, 传入类型 : %v", splitType)
|
return errors.Wrapf(errors.New("日志切割时间类型错误"), "日志切割时间类型错误, 传入类型 : %v", splitType)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
19
logger.go
19
logger.go
@ -8,8 +8,10 @@
|
|||||||
package logger
|
package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.zhangdeman.cn/zhangdeman/consts"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
@ -25,11 +27,26 @@ import (
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 5:05 下午 2021/1/2
|
// 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 {
|
if nil == splitConfig {
|
||||||
return nil, errors.New("未配置日志切割规则")
|
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{}
|
o := &OptionLogger{}
|
||||||
|
|
||||||
for _, f := range optionFunc {
|
for _, f := range optionFunc {
|
||||||
|
Loading…
Reference in New Issue
Block a user