Ai
1 Star 1 Fork 0

博珺懿蕭/go-base-util
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
root.go 3.19 KB
一键复制 编辑 原始数据 按行查看 历史
xiaoby 提交于 2022-03-02 17:09 +08:00 . 新增基于zaplog封装的日志处理库
package log
import (
"fmt"
"io"
"os"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
// LoggerParam 日志参数
type LoggerParam struct {
path string // 日志文件路径
maxSize int // 日志最大存储量
maxBackups int // 日志最大备份数
maxAge int // 日志最大存储天数
compress bool // 是否压缩
logs map[string]Level // 日志map
}
/*
NewLogger 创建日志参数对象
@param path 日志文件路径
@param maxSize 日志最大存储量
@param maxBackups 日志最大备份数
@param maxAge 日志最大存储天数
@param compress 是否压缩
@return 日志参数对象
*/
func NewLogger(path string, maxSize, maxBackups, maxAge int, compress bool) *LoggerParam {
return &LoggerParam{
path: path,
maxSize: maxSize,
maxBackups: maxBackups,
maxAge: maxAge,
compress: compress,
}
}
/*
New 创建Uber日志对象
@param logs 日志文件
@return uber日志对象
*/
func (log *LoggerParam) New(logs map[string]Level) *zap.SugaredLogger {
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "log",
CallerKey: "lineNo",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: func(time time.Time, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString(time.Format("[2006-01-02 15:04:05]"))
},
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.FullCallerEncoder, // 全路径编码器
EncodeName: zapcore.FullNameEncoder,
}
// infoLevel := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
// return level < zapcore.WarnLevel && level >= zap.InfoLevel
// })
cores := make([]zapcore.Core, 0)
for name := range logs {
// logs[name]
writer := GetWrite(fmt.Sprintf("%s/%s", log.path, name), log.maxSize, log.maxBackups, log.maxAge, log.compress)
level := zap.LevelEnablerFunc(logs[name])
cores = append(cores, zapcore.NewCore(zapcore.NewConsoleEncoder(encoderConfig), zapcore.AddSync(writer), level))
}
cores = append(cores, zapcore.NewCore(zapcore.NewConsoleEncoder(encoderConfig), zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout)), zap.InfoLevel))
core := zapcore.NewTee(
// zapcore.NewCore(zapcore.NewConsoleEncoder(encoderConfig), zapcore.AddSync(log.getWrite(log.InfoName)), infoLevel),
// zapcore.NewCore(zapcore.NewConsoleEncoder(encoderConfig), zapcore.AddSync(log.getWrite(log.ErrorName)), errorLevel),
// json log
// zapcore.NewCore(zapcore.NewJSONEncoder(encoderConfig), zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout)), zap.InfoLevel),
// console log
cores...,
)
caller := zap.AddCaller()
development := zap.Development()
return zap.New(core, caller, development, zap.Fields()).Sugar()
}
func GetWrite(path string, maxSize, maxBackups, maxAge int, compress bool) io.Writer {
return &lumberjack.Logger{
Filename: path, // 文件路径
MaxSize: maxSize, // 日志最大存储量
MaxBackups: maxBackups, // 日志最大备份数
MaxAge: maxAge, // 日志最大存储天数
Compress: compress, // 是否压缩
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/miajio/go-base-util.git
git@gitee.com:miajio/go-base-util.git
miajio
go-base-util
go-base-util
v1.0.6

搜索帮助