package c_log

import (
	"cicv-data-closedloop/common/util"
	"fmt"
	"github.com/sirupsen/logrus"
	"os"
	"path/filepath"
	"runtime"
	"time"
)

var GlobalLogger *logrus.Logger

// InitLog 初始化日志配置
func InitLog(logDir string, prefix string) {
	time.Sleep(time.Duration(1) * time.Second)
	// 创建、追加、读写，777，所有权限
	logPath := logDir + prefix + "-" + time.Now().Format("2006-01-02-15-04-05") + ".log"
	err := util.CreateParentDir(logPath)
	if err != nil {
		os.Exit(-1)
	}
	f, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_RDWR, os.ModePerm)
	if err != nil {
		os.Exit(-1)
	}
	GlobalLogger = logrus.New()
	GlobalLogger.SetOutput(f)
	GlobalLogger.SetReportCaller(true) // 开启行号显示
	GlobalLogger.SetFormatter(&logrus.JSONFormatter{
		CallerPrettyfier: func(frame *runtime.Frame) (string, string) {
			fileName := filepath.Base(frame.File)
			return "", fmt.Sprintf("%s:%d", fileName, frame.Line)
		},
	})
	GlobalLogger.Info("初始化GlobalLogger - 成功")

}
