|
|
|
@ -1,12 +1,10 @@
|
|
|
|
|
package logger
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
zaprotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
|
"io"
|
|
|
|
|
"os"
|
|
|
|
|
"path"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
@ -43,28 +41,22 @@ var (
|
|
|
|
|
type logger struct {
|
|
|
|
|
*zap.Logger
|
|
|
|
|
Config *LoggerConfig
|
|
|
|
|
hasShowLine bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type LoggerConfig struct {
|
|
|
|
|
Director string `json:"director"`
|
|
|
|
|
Levels []zapcore.Level `json:"level"`
|
|
|
|
|
ShowLine bool `json:"showLine"`
|
|
|
|
|
StacktraceKey string `json:"stacktraceKey"`
|
|
|
|
|
LinkName string `json:"linkName"`
|
|
|
|
|
LogInConsole bool `json:"logInConsole"`
|
|
|
|
|
Format string `json:"format"`
|
|
|
|
|
EncodeLevel zapcore.LevelEncoder `json:"encodeLevel"`
|
|
|
|
|
Prefix string `json:"prefix"`
|
|
|
|
|
MaxAge uint `json:"maxAge"`
|
|
|
|
|
Writer func(config *LoggerConfig, filename string, level zapcore.Level) io.Writer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InitLogger @Title 初始化日志工具
|
|
|
|
|
func InitLogger(config *LoggerConfig) *logger {
|
|
|
|
|
Logger.Config = config
|
|
|
|
|
if ok, _ := Logger.pathExists(config.Director); !ok { // 判断是否有Director文件夹
|
|
|
|
|
fmt.Printf("create %v directory\n", config.Director)
|
|
|
|
|
_ = os.Mkdir(config.Director, os.ModePerm)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.Logger = zap.New(zapcore.NewTee(
|
|
|
|
|
Logger.getEncoderCore("debug", zapcore.DebugLevel),
|
|
|
|
@ -86,7 +78,6 @@ func (l *logger) getEncoderConfig() (config zapcore.EncoderConfig) {
|
|
|
|
|
TimeKey: "time",
|
|
|
|
|
NameKey: "logger",
|
|
|
|
|
CallerKey: "caller",
|
|
|
|
|
StacktraceKey: l.Config.StacktraceKey,
|
|
|
|
|
LineEnding: zapcore.DefaultLineEnding,
|
|
|
|
|
EncodeLevel: l.Config.EncodeLevel,
|
|
|
|
|
EncodeTime: l.CustomTimeEncoder,
|
|
|
|
@ -109,12 +100,7 @@ func (l *logger) getEncoder() zapcore.Encoder {
|
|
|
|
|
|
|
|
|
|
// getEncoderCore 获取Encoder的zapcore.Core
|
|
|
|
|
func (l *logger) getEncoderCore(filename string, level zapcore.Level) (core zapcore.Core) {
|
|
|
|
|
writer, err := l.getWriteSyncer(filename) // 使用file-rotatelogs进行日志分割
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Get Write Syncer Failed err:%v", err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return zapcore.NewCore(l.getEncoder(), writer, zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
|
|
|
|
|
return zapcore.NewCore(l.getEncoder(), l.getWriteSyncer(filename, level), zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
|
|
|
|
|
return l.inLevel(level) && lvl >= level
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
@ -149,20 +135,17 @@ func (l *logger) pathExists(path string) (bool, error) {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *logger) getWriteSyncer(filename string) (zapcore.WriteSyncer, error) {
|
|
|
|
|
options := []zaprotatelogs.Option{
|
|
|
|
|
zaprotatelogs.WithLinkName(l.Config.LinkName),
|
|
|
|
|
zaprotatelogs.WithRotationTime(time.Hour * 24),
|
|
|
|
|
}
|
|
|
|
|
if l.Config.MaxAge > 0 {
|
|
|
|
|
options = append(options, zaprotatelogs.WithMaxAge(time.Hour*24*time.Duration(l.Config.MaxAge)))
|
|
|
|
|
}
|
|
|
|
|
fileWriter, err := zaprotatelogs.New(
|
|
|
|
|
path.Join(l.Config.Director, filename+".%Y-%m-%d.log"),
|
|
|
|
|
options...,
|
|
|
|
|
)
|
|
|
|
|
if l.Config.LogInConsole {
|
|
|
|
|
return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter)), err
|
|
|
|
|
}
|
|
|
|
|
return zapcore.AddSync(fileWriter), err
|
|
|
|
|
func (l *logger) getWriteSyncer(filename string, level zapcore.Level) zapcore.WriteSyncer {
|
|
|
|
|
var fileWriter io.Writer
|
|
|
|
|
// 日志切割
|
|
|
|
|
if l.Config.Writer != nil {
|
|
|
|
|
fileWriter = l.Config.Writer(l.Config, filename, level)
|
|
|
|
|
} else {
|
|
|
|
|
return zapcore.AddSync(os.Stdout)
|
|
|
|
|
}
|
|
|
|
|
if l.Config.LogInConsole && !l.hasShowLine && l.inLevel(level) {
|
|
|
|
|
l.hasShowLine = true
|
|
|
|
|
return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter))
|
|
|
|
|
}
|
|
|
|
|
return zapcore.AddSync(fileWriter)
|
|
|
|
|
}
|
|
|
|
|