package logs

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

type commonLogConfig struct {
	Path   string //系统日志默认文件默认报错路径。默认为./logs
	Debug  bool   //是否打印调试信息。默认false
	Stdout bool   //是否输出到控制台。默认false
}

func initBaseLog(c *commonLogConfig, fileName string) (l *glog.Logger) {
	l = glog.New()
	_ = l.SetPath(c.Path)
	l.SetFile(fileName)
	l.SetDebug(c.Debug)
	l.SetStdoutPrint(c.Stdout)
	return
}

// 请求记录配置
type serverLogConfig struct {
	*commonLogConfig
	ServerErrorStack       bool   //当Server捕获到异常时是否记录堆栈信息到日志中。默认为true
	ServerErrorLogEnabled  bool   //是否记录访问异常日志到日志中。默认为true
	ServerErrorLogPattern  string //访问异常日志文件格式。默认为"error-{Ymd}.log"
	ServerAccessLogEnabled bool   //是否记录访问日志。默认为false
	ServerAccessLogPattern string //记录访问日志文件格式。默认为"access-{Ymd}.log"
	ServerRequestTimeout   int64
}

// server 请求日志
type serverLog struct {
	accessLog, errorLog *glog.Logger
	Config              serverLogConfig
	Notice              *Notice
}

func initServerLog(sc serverLogConfig, nc NoticeConfig) *serverLog {
	accessLog, errorLog := glog.New(), glog.New()
	_ = accessLog.SetPath(sc.Path)
	_ = errorLog.SetPath(sc.Path)

	accessLog.SetFile(sc.ServerAccessLogPattern)
	errorLog.SetFile(sc.ServerErrorLogPattern)

	accessLog.SetDebug(sc.Debug)
	errorLog.SetDebug(sc.Debug)

	accessLog.SetStdoutPrint(sc.Stdout)
	errorLog.SetStdoutPrint(sc.Stdout)

	notice, err := newNotice(nc)
	if err != nil {
		log.Println("GNotice nsq通知初始化异常", err)
	}

	return &serverLog{
		accessLog: accessLog,
		errorLog:  errorLog,
		Config:    sc,
		Notice:    notice,
	}
}
