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

View File

@ -12,7 +12,6 @@ import (
"fmt"
"os"
"strings"
"time"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/network/util"
@ -78,22 +77,22 @@ func (ld *LogData) ToFieldList() []zap.Field {
// InputLogConfig 输入的日志配置
type InputLogConfig struct {
Name string `json:"name" yaml:"name"` // 日志文件名
Path string `json:"path" yaml:"path"` // 日志文件路径
TimeIntervalType consts.LogSplit `json:"time_interval_type" yaml:"time_interval_type"` // 日志切割规则
DivisionChar string `json:"division_char" yaml:"division_char"` // 文件名分隔符
LogLevel consts.LogLevel `json:"log_level" yaml:"log_level"` // 日志等级
Console bool `json:"console" yaml:"console"` // 是否进行控制台日志输出
UseJson bool `json:"use_json" yaml:"use_json"` // 日志是否使用JSON格式
FileLine bool `json:"file_line" yaml:"file_line"` // 日志是否打印行号
MessageKey string `json:"message_key" yaml:"message_key"` // message 字段
LevelKey string `json:"level_key" yaml:"level_key"` // level 字段
TimeKey string `json:"time_key" yaml:"time_key"` // 时间字段
CallerKey string `json:"caller_key" yaml:"caller_key"` // 记录日志的文件的代码行数
UseShortFile bool `json:"use_short_file" yaml:"use_short_file"` // 是否使用短文件格式
CallerSkip int `json:"caller_skip" yaml:"caller_skip"` // 日志记录的文件跳过多少层
MaxAge int `json:"max_age" yaml:"max_age"` // 日志最长保存时间, 单位 : 秒
ZincSyncConfig *ZincConfig `json:"zinc_sync_config" yaml:"zinc_sync_config"` // 日志同步至zinc的配置
Name string `json:"name" yaml:"name"` // 日志文件名
Path string `json:"path" yaml:"path"` // 日志文件路径
TimeIntervalType LogSplit `json:"time_interval_type" yaml:"time_interval_type"` // 日志切割规则
DivisionChar string `json:"division_char" yaml:"division_char"` // 文件名分隔符
LogLevel LogLevel `json:"log_level" yaml:"log_level"` // 日志等级
Console bool `json:"console" yaml:"console"` // 是否进行控制台日志输出
UseJson bool `json:"use_json" yaml:"use_json"` // 日志是否使用JSON格式
FileLine bool `json:"file_line" yaml:"file_line"` // 日志是否打印行号
MessageKey string `json:"message_key" yaml:"message_key"` // message 字段
LevelKey string `json:"level_key" yaml:"level_key"` // level 字段
TimeKey string `json:"time_key" yaml:"time_key"` // 时间字段
CallerKey string `json:"caller_key" yaml:"caller_key"` // 记录日志的文件的代码行数
UseShortFile bool `json:"use_short_file" yaml:"use_short_file"` // 是否使用短文件格式
CallerSkip int `json:"caller_skip" yaml:"caller_skip"` // 日志记录的文件跳过多少层
MaxAge int64 `json:"max_age" yaml:"max_age"` // 日志最长保存时间, 单位 : 秒
ZincSyncConfig *ZincConfig `json:"zinc_sync_config" yaml:"zinc_sync_config"` // 日志同步至zinc的配置
}
// NewLogData ...
@ -136,10 +135,6 @@ func getStrVal(ctx context.Context, key string) string {
}
// GetLogInstanceFromInputConfig 从输入配置获取日志实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:43 2022/6/12
func GetLogInstanceFromInputConfig(logConf *InputLogConfig) (*zap.Logger, error) {
if nil == logConf {
return nil, nil
@ -166,7 +161,7 @@ func GetLogInstanceFromInputConfig(logConf *InputLogConfig) (*zap.Logger, error)
logConf.Name,
WithDivisionChar(logConf.DivisionChar),
WithTimeIntervalType(logConf.TimeIntervalType),
WithMaxAge(time.Duration(logConf.MaxAge)*time.Second)); nil != err {
WithMaxAge(logConf.MaxAge)); nil != err {
return nil, err
}
if loggerInstance, err = NewLogger(logConf.LogLevel, splitConfig, logConfList...); nil != err {
@ -176,27 +171,23 @@ func GetLogInstanceFromInputConfig(logConf *InputLogConfig) (*zap.Logger, error)
}
// inputLevel2ZapLevel 输入日志等级转化为zap日志等级
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:07 2024/6/24
func inputLevel2ZapLevel(inputLoggerLevel consts.LogLevel) zapcore.Level {
inputLoggerLevel = consts.LogLevel(strings.ToUpper(inputLoggerLevel.String()))
func inputLevel2ZapLevel(inputLoggerLevel LogLevel) zapcore.Level {
inputLoggerLevel = LogLevel(strings.ToUpper(inputLoggerLevel.String()))
if !inputLoggerLevel.IsValid() {
// 非法的日志等级, 自动重定向为 info 级别
inputLoggerLevel = consts.LogLevelInfo
inputLoggerLevel = LogLevelInfo
}
loggerLevel := zapcore.DebugLevel
switch inputLoggerLevel {
case consts.LogLevelDebug:
case LogLevelDebug:
loggerLevel = zapcore.DebugLevel
case consts.LogLevelInfo:
case LogLevelInfo:
loggerLevel = zapcore.InfoLevel
case consts.LogLevelWarn:
case LogLevelWarn:
loggerLevel = zapcore.WarnLevel
case consts.LogLevelError:
case LogLevelError:
loggerLevel = zapcore.ErrorLevel
case consts.LogLevelPanic:
case LogLevelPanic:
loggerLevel = zapcore.PanicLevel
}
return loggerLevel