package logs

import (
	"github.com/gogf/gf/v2/os/gcfg"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/os/glog"
	"log"
)

var GInfo *glog.Logger // 系统日志输出
var GReq *serverLog    // 服务请求记录

func InitLogs() {
	ctx := gctx.New()
	logCommon := &commonLogConfig{
		Path:   gcfg.Instance().MustGet(ctx, "system.log.path", "./logs").String(),
		Debug:  gcfg.Instance().MustGet(ctx, "system.log.debug", false).Bool(),
		Stdout: gcfg.Instance().MustGet(ctx, "system.log.stdout", false).Bool(),
	}
	//初始化GInfo
	gInfoName := gcfg.Instance().MustGet(ctx, "system.log.pattern", "system-{Ymd}.log").String()
	GInfo = initBaseLog(logCommon, gInfoName)
	//设置系统默认日志
	glog.SetDefaultLogger(GInfo)
	//将log输出转至GInfo
	log.SetOutput(GInfo)

	//初始化通知
	var noticeConfig NoticeConfig
	if err := gcfg.Instance().MustGet(ctx, "system.alarm").Scan(&noticeConfig); err != nil {
		GInfo.Errorf(ctx, "nsq通知配置异常", err)
	}

	//初始化请求记录
	GReq = initServerLog(serverLogConfig{
		commonLogConfig:        logCommon,
		ServerErrorStack:       gcfg.Instance().MustGet(ctx, "system.log.serverErrorStack", false).Bool(),
		ServerErrorLogEnabled:  gcfg.Instance().MustGet(ctx, "system.log.serverErrorLogEnabled", false).Bool(),
		ServerErrorLogPattern:  gcfg.Instance().MustGet(ctx, "system.log.serverErrorLogPattern", "system-{Ymd}.log").String(),
		ServerAccessLogEnabled: gcfg.Instance().MustGet(ctx, "system.log.serverAccessLogEnabled", false).Bool(),
		ServerAccessLogPattern: gcfg.Instance().MustGet(ctx, "system.log.serverAccessLogPattern", "system-{Ymd}.log").String(),
		ServerRequestTimeout:   gcfg.Instance().MustGet(ctx, "system.log.serverRequestTimeout", 500).Int64(),
	}, noticeConfig)

}
