update logger
This commit is contained in:
parent
408f33b21e
commit
c4b4c68394
30
define.go
30
define.go
@ -8,6 +8,8 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
@ -22,9 +24,9 @@ import (
|
||||
type InputLogConfig struct {
|
||||
Name string `json:"name" yaml:"name"` // 日志文件名
|
||||
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"` // 文件名分隔符
|
||||
LogLevel int `json:"log_level" yaml:"log_level"` // 日志等级
|
||||
LogLevel string `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"` // 日志是否打印行号
|
||||
@ -69,8 +71,30 @@ func GetLogInstanceFromInputConfig(logConf *InputLogConfig) (*zap.Logger, error)
|
||||
WithMaxAge(time.Duration(logConf.MaxAge)*time.Second)); 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 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
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"github.com/pkg/errors"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
@ -32,21 +29,8 @@ func NewLogger(inputLoggerLevel string, splitConfig *RotateLogConfig, optionFunc
|
||||
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")
|
||||
}
|
||||
loggerLevel := inputLevel2ZapLevel(inputLoggerLevel)
|
||||
|
||||
o := &OptionLogger{}
|
||||
|
||||
for _, f := range optionFunc {
|
||||
|
Loading…
Reference in New Issue
Block a user