bi/engine/logs/log.go

116 lines
2.9 KiB
Go
Raw Normal View History

2025-05-22 16:27:39 +08:00
// 日志引擎层
2022-01-26 16:40:50 +08:00
package logs
import (
"errors"
"fmt"
"io"
"log"
2025-05-22 16:27:39 +08:00
"os"
2022-01-26 16:40:50 +08:00
"path/filepath"
"time"
"github.com/1340691923/xwl_bi/platform-basic-libs/util"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var Logger *zap.Logger
type Log struct {
logPath string
storageDays int
}
2025-05-22 16:27:39 +08:00
// Options方法
2022-01-26 16:40:50 +08:00
type NewLogOptions func(log *Log)
2025-05-22 16:27:39 +08:00
// 设置日志目录
2022-01-26 16:40:50 +08:00
func WithLogPath(logPath string) NewLogOptions {
return func(log *Log) {
log.logPath = logPath
}
}
2025-05-22 16:27:39 +08:00
// 设置日志存活天数
2022-01-26 16:40:50 +08:00
func WithStorageDays(storageDays int) NewLogOptions {
return func(log *Log) {
log.storageDays = storageDays
}
}
2025-05-22 16:27:39 +08:00
// App 构造方法
2022-01-26 16:40:50 +08:00
func NewLog(opts ...NewLogOptions) *Log {
log := &Log{
logPath: filepath.Join(util.GetCurrentDirectory(), "logs"),
storageDays: 3,
}
for _, opt := range opts {
opt(log)
}
return log
}
// 初始化日志 logger
func (this *Log) InitLog() (logger *zap.Logger, err error) {
config := zapcore.EncoderConfig{
MessageKey: "msg",
TimeKey: "ts",
EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05"))
},
CallerKey: "file",
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
}
encoder := zapcore.NewConsoleEncoder(config)
infoLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl == zapcore.InfoLevel
})
warnLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.WarnLevel && lvl >= zap.InfoLevel
})
2025-05-22 16:27:39 +08:00
allLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.DebugLevel
})
2022-01-26 16:40:50 +08:00
infoWriter, err := this.getWriter(filepath.Join(this.logPath, "info.log"), this.storageDays)
if err != nil {
return nil, errors.New(fmt.Sprintf("日志启动异常:%s", err))
}
warnWriter, err := this.getWriter(filepath.Join(this.logPath, "err.log"), this.storageDays)
if err != nil {
return nil, errors.New(fmt.Sprintf("日志启动异常:%s", err))
}
var core zapcore.Core
core = zapcore.NewTee(
zapcore.NewCore(encoder, zapcore.AddSync(infoWriter), infoLevel),
zapcore.NewCore(encoder, zapcore.AddSync(warnWriter), warnLevel),
2025-05-22 16:27:39 +08:00
zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), allLevel),
2022-01-26 16:40:50 +08:00
)
return zap.New(core, zap.AddCaller(), zap.Development()), nil
}
func (this *Log) getWriter(filename string, storageDays int) (io.Writer, error) {
// 生成rotatelogs的Logger 实际生成的文件名 info.log.YYmmddHH
hook, err := rotatelogs.New(
filename+".%Y%m%d", // 没有使用go风格反人类的format格式
rotatelogs.WithLinkName(filename),
rotatelogs.WithMaxAge(time.Hour*24*time.Duration(storageDays)), // 保存3天
rotatelogs.WithRotationTime(time.Hour*24), //切割频率 24小时
)
if err != nil {
return nil, errors.New(fmt.Sprintf("日志启动异常:%s", err))
}
return hook, nil
}
func Debug(format string, v ...interface{}) {
log.Println(format, v)
}