package initApp

import (
	"DataShare/global"
	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
	"github.com/rifflock/lfshook"
	"github.com/sirupsen/logrus"
	path "path"
	"time"
)

func LoggerInit() error{
	global.SystemLogger = logrus.New()

	global.SystemLogger.SetLevel(logrus.Level(global.SystemConfig.Log.Level))
	fileName := path.Join(global.SystemConfig.Log.Path, global.SystemConfig.Log.Name)

	// 设置 rotatelogs,实现文件分割
	logWriter, _ := rotatelogs.New(
		// 分割后的文件名称
		fileName+".%Y%m%d.log",
		// 生成软链，指向最新日志文件
		rotatelogs.WithLinkName(fileName),
		// 设置最大保存时间(7天)
		rotatelogs.WithMaxAge(7*24*time.Hour), //以hour为单位的整数
		// 设置日志切割时间间隔(1天)
		rotatelogs.WithRotationTime(1*time.Hour),
	)
	//hook机制的设置
	writerMap := lfshook.WriterMap{
		logrus.InfoLevel:  logWriter,
		logrus.FatalLevel: logWriter,
		logrus.DebugLevel: logWriter,
		logrus.WarnLevel:  logWriter,
		logrus.ErrorLevel: logWriter,
		logrus.PanicLevel: logWriter,
	}
	//给logrus添加hook
	global.SystemLogger.AddHook(lfshook.NewHook(writerMap,&logrus.JSONFormatter{
		TimestampFormat:   "2006-01-02 15:04:05",
	}))

	return nil
}

