update logger
This commit is contained in:
		
							
								
								
									
										56
									
								
								define.go
									
									
									
									
									
								
							
							
						
						
									
										56
									
								
								define.go
									
									
									
									
									
								
							| @ -8,6 +8,8 @@ | |||||||
| package logger | package logger | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"git.zhangdeman.cn/zhangdeman/consts" | ||||||
|  | 	"strings" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| 	"go.uber.org/zap" | 	"go.uber.org/zap" | ||||||
| @ -20,21 +22,21 @@ import ( | |||||||
| // | // | ||||||
| // Date : 18:40 2022/6/12 | // Date : 18:40 2022/6/12 | ||||||
| type InputLogConfig struct { | type InputLogConfig struct { | ||||||
| 	Name             string           `json:"name" yaml:"name"`                             // 日志文件名 | 	Name             string `json:"name" yaml:"name"`                             // 日志文件名 | ||||||
| 	Path             string           `json:"path" yaml:"path"`                             // 日志文件路径 | 	Path             string `json:"path" yaml:"path"`                             // 日志文件路径 | ||||||
| 	TimeIntervalType TimeIntervalType `json:"time_interval_type" yaml:"time_interval_type"` // 日志切割规则 | 	TimeIntervalType string `json:"time_interval_type" yaml:"time_interval_type"` // 日志切割规则 | ||||||
| 	DivisionChar     string           `json:"division_char" yaml:"division_char"`           // 文件名分隔符 | 	DivisionChar     string `json:"division_char" yaml:"division_char"`           // 文件名分隔符 | ||||||
| 	LogLevel         int              `json:"log_level" yaml:"log_level"`                   // 日志等级 | 	LogLevel         string `json:"log_level" yaml:"log_level"`                   // 日志等级 | ||||||
| 	Console          bool             `json:"console" yaml:"console"`                       // 是否进行控制台日志输出 | 	Console          bool   `json:"console" yaml:"console"`                       // 是否进行控制台日志输出 | ||||||
| 	UseJson          bool             `json:"use_json" yaml:"use_json"`                     // 日志是否使用JSON格式 | 	UseJson          bool   `json:"use_json" yaml:"use_json"`                     // 日志是否使用JSON格式 | ||||||
| 	FileLine         bool             `json:"file_line" yaml:"file_line"`                   // 日志是否打印行号 | 	FileLine         bool   `json:"file_line" yaml:"file_line"`                   // 日志是否打印行号 | ||||||
| 	MessageKey       string           `json:"message_key" yaml:"message_key"`               // message 字段 | 	MessageKey       string `json:"message_key" yaml:"message_key"`               // message 字段 | ||||||
| 	LevelKey         string           `json:"level_key" yaml:"level_key"`                   // level 字段 | 	LevelKey         string `json:"level_key" yaml:"level_key"`                   // level 字段 | ||||||
| 	TimeKey          string           `json:"time_key" yaml:"time_key"`                     // 时间字段 | 	TimeKey          string `json:"time_key" yaml:"time_key"`                     // 时间字段 | ||||||
| 	CallerKey        string           `json:"caller_key" yaml:"caller_key"`                 // 记录日志的文件的代码行数 | 	CallerKey        string `json:"caller_key" yaml:"caller_key"`                 // 记录日志的文件的代码行数 | ||||||
| 	UseShortFile     bool             `json:"use_short_file" yaml:"use_short_file"`         // 是否使用短文件格式 | 	UseShortFile     bool   `json:"use_short_file" yaml:"use_short_file"`         // 是否使用短文件格式 | ||||||
| 	CallerSkip       int              `json:"caller_skip" yaml:"caller_skip"`               // 日志记录的文件跳过多少层 | 	CallerSkip       int    `json:"caller_skip" yaml:"caller_skip"`               // 日志记录的文件跳过多少层 | ||||||
| 	MaxAge           int              `json:"max_age" yaml:"max_age"`                       // 日志最长保存时间, 单位 : 秒 | 	MaxAge           int    `json:"max_age" yaml:"max_age"`                       // 日志最长保存时间, 单位 : 秒 | ||||||
| } | } | ||||||
|  |  | ||||||
| // GetLogInstanceFromInputConfig 从输入配置获取日志实例 | // GetLogInstanceFromInputConfig 从输入配置获取日志实例 | ||||||
| @ -69,8 +71,30 @@ func GetLogInstanceFromInputConfig(logConf *InputLogConfig) (*zap.Logger, error) | |||||||
| 		WithMaxAge(time.Duration(logConf.MaxAge)*time.Second)); nil != err { | 		WithMaxAge(time.Duration(logConf.MaxAge)*time.Second)); nil != err { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	if loggerInstance, err = NewLogger(zapcore.Level(logConf.LogLevel), splitConfig, logConfList...); nil != err { | 	if loggerInstance, err = NewLogger(logConf.LogLevel, splitConfig, logConfList...); nil != err { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	return loggerInstance, nil | 	return loggerInstance, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // inputLevel2ZapLevel 输入日志等级转化为zap日志等级 | ||||||
|  | // | ||||||
|  | // Author : go_developer@163.com<白茶清欢> | ||||||
|  | // | ||||||
|  | // Date : 18:07 2024/6/24 | ||||||
|  | func inputLevel2ZapLevel(inputLoggerLevel string) zapcore.Level { | ||||||
|  | 	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 | ||||||
|  | 	} | ||||||
|  | 	return loggerLevel | ||||||
|  | } | ||||||
|  | |||||||
							
								
								
									
										22
									
								
								logger.go
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								logger.go
									
									
									
									
									
								
							| @ -8,12 +8,9 @@ | |||||||
| package logger | package logger | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"git.zhangdeman.cn/zhangdeman/consts" | 	"github.com/pkg/errors" | ||||||
| 	"io" | 	"io" | ||||||
| 	"os" | 	"os" | ||||||
| 	"strings" |  | ||||||
|  |  | ||||||
| 	"github.com/pkg/errors" |  | ||||||
|  |  | ||||||
| 	"go.uber.org/zap" | 	"go.uber.org/zap" | ||||||
|  |  | ||||||
| @ -32,21 +29,8 @@ func NewLogger(inputLoggerLevel string, splitConfig *RotateLogConfig, optionFunc | |||||||
| 		return nil, errors.New("未配置日志切割规则") | 		return nil, errors.New("未配置日志切割规则") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	loggerLevel := zapcore.DebugLevel | 	loggerLevel := inputLevel2ZapLevel(inputLoggerLevel) | ||||||
| 	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 { | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user