feat: 优化升级日志组织

This commit is contained in:
2025-10-30 19:42:06 +08:00
parent 9001cd0048
commit 1b81309d82
6 changed files with 179 additions and 206 deletions

126
config.go
View File

@ -14,7 +14,6 @@ import (
"strings"
"time"
"git.zhangdeman.cn/zhangdeman/consts"
"go.uber.org/zap/zapcore"
)
@ -27,43 +26,27 @@ const (
)
// RotateLogConfig 日志切割的配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:08 下午 2021/1/2
type RotateLogConfig struct {
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"` // 日志最长保存时间
TimeIntervalType 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 int64 `json:"max_age" yaml:"max_age"` // 日志最长保存时间, 单位: s
}
// SetRotateLogConfigFunc 设置日志切割的选项
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:13 下午 2021/1/2
type SetRotateLogConfigFunc func(rlc *RotateLogConfig)
// WithTimeIntervalType 设置日志切割时间间隔
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:34 下午 2021/1/2
func WithTimeIntervalType(timeIntervalType consts.LogSplit) SetRotateLogConfigFunc {
func WithTimeIntervalType(timeIntervalType LogSplit) SetRotateLogConfigFunc {
return func(rlc *RotateLogConfig) {
rlc.TimeIntervalType = timeIntervalType
}
}
// WithDivisionChar 设置分隔符
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:49 下午 2021/1/2
func WithDivisionChar(divisionChar string) SetRotateLogConfigFunc {
return func(rlc *RotateLogConfig) {
rlc.DivisionChar = divisionChar
@ -71,27 +54,22 @@ func WithDivisionChar(divisionChar string) SetRotateLogConfigFunc {
}
// WithMaxAge 设置日志保存时间
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 5:03 下午 2021/1/2
func WithMaxAge(maxAge time.Duration) SetRotateLogConfigFunc {
func WithMaxAge(maxAge int64) SetRotateLogConfigFunc {
return func(rlc *RotateLogConfig) {
if maxAge <= 0 {
maxAge = 3 * 24 * 3600 // 默认3天
}
rlc.MaxAge = maxAge
}
}
// NewRotateLogConfig 生成日志切割的配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:53 下午 2021/1/2
func NewRotateLogConfig(logPath string, logFile string, option ...SetRotateLogConfigFunc) (*RotateLogConfig, error) {
if len(logPath) == 0 || len(logFile) == 0 {
return nil, LogPathEmptyError()
}
c := &RotateLogConfig{
TimeIntervalType: consts.LogSplitHour,
TimeIntervalType: LogSplitHour,
LogPath: logPath,
LogFileName: logFile,
DivisionChar: "",
@ -109,10 +87,6 @@ func NewRotateLogConfig(logPath string, logFile string, option ...SetRotateLogCo
}
// formatConfig 格式化配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:23 下午 2021/1/2
func formatConfig(c *RotateLogConfig) error {
if len(c.DivisionChar) == 0 {
@ -132,23 +106,23 @@ func formatConfig(c *RotateLogConfig) error {
}
}
c.TimeIntervalType = consts.LogSplit(strings.ToUpper(c.TimeIntervalType.String()))
c.TimeIntervalType = LogSplit(strings.ToUpper(c.TimeIntervalType.String()))
if !c.TimeIntervalType.IsValid() {
// 非法的日志切割规则,默认按天切
c.TimeIntervalType = consts.LogSplitDay
c.TimeIntervalType = LogSplitDay
}
// 生成格式化日志全路径
switch c.TimeIntervalType {
case consts.LogSplitHour:
case LogSplitHour:
c.TimeInterval = time.Hour
c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + "%m" + c.DivisionChar + "%d" + c.DivisionChar + "%H" + c.DivisionChar + c.LogFileName
case consts.LogSplitDay:
case LogSplitDay:
c.TimeInterval = time.Hour * 24
c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + "%m" + c.DivisionChar + "%d" + c.DivisionChar + c.LogFileName
case consts.LogSplitMonth:
case LogSplitMonth:
c.TimeInterval = time.Hour * 24 * 30
c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + "%m" + c.DivisionChar + c.LogFileName
case consts.LogSplitYear:
case LogSplitYear:
c.TimeInterval = time.Hour * 24 * 365
c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + c.LogFileName
default:
@ -176,51 +150,31 @@ const (
)
// defaultTimeEncoder 默认的时间处理
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:53 下午 2021/1/2
func defaultTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
sec := t.UnixNano() / 1e9
ms := t.UnixNano() / 1e6 % 1e3
ns := t.UnixNano() % 1e6
enc.AppendString(time.Unix(sec, ns).Format("2006-01-02 15:04:05") + "." + fmt.Sprintf("%v", ms) + "+" + fmt.Sprintf("%v", ns))
enc.AppendString(time.Unix(sec, ns).Format(time.DateTime) + "." + fmt.Sprintf("%v", ms) + "+" + fmt.Sprintf("%v", ns))
}
// SecondTimeEncoder 秒级时间戳格式化
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:34 下午 2021/1/3
func SecondTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05"))
enc.AppendString(t.Format(time.DateTime))
}
// MsTimeEncoder 毫秒时间格式化方法
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:35 下午 2021/1/3
func MsTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
sec := t.UnixNano() / 1e9
ms := t.UnixNano() / 1e6 % 1e3
enc.AppendString(time.Unix(sec, 0).Format("2006-01-02 15:04:05") + "." + fmt.Sprintf("%v", ms))
enc.AppendString(time.Unix(sec, 0).Format(time.DateTime) + "." + fmt.Sprintf("%v", ms))
}
// defaultEncodeDuration 默认的原始时间处理
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:56 下午 2021/1/2
func defaultEncodeDuration(d time.Duration, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendInt64(int64(d) / 1000000)
}
// OptionLogger 日志配置的选项
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:41 下午 2021/1/2
type OptionLogger struct {
UseJsonFormat bool // 日志使用json格式
MessageKey string // message 字段
@ -269,10 +223,6 @@ func WithEncoder(encoder zapcore.Encoder) SetLoggerOptionFunc {
}
// WithUseJsonFormat 日志是否使用json格式数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:30 上午 2021/1/3
func WithUseJsonFormat(isJsonFormat bool) SetLoggerOptionFunc {
return func(o *OptionLogger) {
o.UseJsonFormat = isJsonFormat
@ -280,10 +230,6 @@ func WithUseJsonFormat(isJsonFormat bool) SetLoggerOptionFunc {
}
// WithMessageKey 使用message key
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:32 上午 2021/1/3
func WithMessageKey(messageKey string) SetLoggerOptionFunc {
return func(o *OptionLogger) {
messageKey = strings.Trim(messageKey, " ")
@ -295,10 +241,6 @@ func WithMessageKey(messageKey string) SetLoggerOptionFunc {
}
// WithLevelKey 设置level key
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:33 上午 2021/1/3
func WithLevelKey(levelKey string) SetLoggerOptionFunc {
return func(o *OptionLogger) {
levelKey = strings.Trim(levelKey, " ")
@ -310,10 +252,6 @@ func WithLevelKey(levelKey string) SetLoggerOptionFunc {
}
// WithTimeKey 设置time key ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:34 上午 2021/1/3
func WithTimeKey(timeKey string) SetLoggerOptionFunc {
return func(o *OptionLogger) {
timeKey = strings.Trim(timeKey, " ")
@ -325,10 +263,6 @@ func WithTimeKey(timeKey string) SetLoggerOptionFunc {
}
// WithCallerKey 设置caller key
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:37 上午 2021/1/3
func WithCallerKey(callerKey string) SetLoggerOptionFunc {
return func(o *OptionLogger) {
callerKey = strings.Trim(callerKey, " ")
@ -340,10 +274,6 @@ func WithCallerKey(callerKey string) SetLoggerOptionFunc {
}
// WithShortCaller 是否使用短caller格式
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:39 上午 2021/1/3
func WithShortCaller(useShortCaller bool) SetLoggerOptionFunc {
return func(o *OptionLogger) {
o.UseShortCaller = useShortCaller
@ -351,10 +281,6 @@ func WithShortCaller(useShortCaller bool) SetLoggerOptionFunc {
}
// WithTimeEncoder 设置格式化时间方法
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:41 上午 2021/1/3
func WithTimeEncoder(encoder zapcore.TimeEncoder) SetLoggerOptionFunc {
return func(o *OptionLogger) {
if nil == encoder {
@ -365,10 +291,6 @@ func WithTimeEncoder(encoder zapcore.TimeEncoder) SetLoggerOptionFunc {
}
// WithEncodeDuration 原始时间
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:42 上午 2021/1/3
func WithEncodeDuration(encoder zapcore.DurationEncoder) SetLoggerOptionFunc {
return func(o *OptionLogger) {
if nil == encoder {
@ -386,10 +308,6 @@ func WithZincLogCollect(zincCfg *ZincConfig) SetLoggerOptionFunc {
}
// GetEncoder 获取空中台输出的encoder
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 6:24 下午 2021/1/2
func GetEncoder(option ...SetLoggerOptionFunc) zapcore.Encoder {
ol := &OptionLogger{
UseJsonFormat: defaultUseJsonFormat,