package utils

import (
	"encoding/json"
	"github.com/beego/beego/v2/core/logs"
	"os"
)

var FileLog *logs.BeeLogger
var FileLogData *logs.BeeLogger
var Binlog *logs.BeeLogger

func init() {
	FileLog = logs.NewLogger(1000000)
	FileLog.SetLogger(logs.AdapterFile, `{"filename":"./rdlucklog/eta_api.log"}`)
	FileLog.EnableFuncCallDepth(true)

	FileLogData = logs.NewLogger(1000000)
	FileLogData.SetLogger(logs.AdapterFile, `{"filename":"./rdlucklog/eta_api_data.log"}`)

	//初始化binlog日志
	initBinlog()
}

func initBinlog() {
	//binlog日志
	binLogDir := `./binlog`
	os.MkdirAll(binLogDir, os.ModePerm)
	Binlog = logs.NewLogger(1000000)
	logConfig := getDefaultLogConfig()
	logConfig.FileName = "./binlog/binlog.log"
	logConfig.MaxLines = 10000000
	logConfig.Rotate = true
	b, _ := json.Marshal(logConfig)
	Binlog.SetLogger(logs.AdapterFile, string(b))
	Binlog.EnableFuncCallDepth(true)
}

type logConfig struct {
	FileName string `json:"filename" description:"保存的文件名"`
	MaxLines int    `json:"maxlines"  description:"每个文件保存的最大行数，默认值 1000000"`
	MaxSize  int    `json:"maxsize" description:"每个文件保存的最大尺寸，默认值是 1 << 28, //256 MB"`
	Daily    bool   `json:"daily" description:"是否按照每天 logrotate，默认是 true"`
	MaxDays  int    `json:"maxdays" description:"文件最多保存多少天，默认保存 7 天"`
	Rotate   bool   `json:"rotate" description:"是否开启 logrotate，默认是 true"`
	Level    int    `json:"level" description:"日志保存的时候的级别，默认是 Trace 级别"`
	Color    bool   `json:"color" description:"日志是否输出颜色"`
	//Perm     string `json:"perm" description:"日志文件权限"`
}

func getDefaultLogConfig() logConfig {
	return logConfig{
		FileName: "",
		MaxLines: 0,
		MaxSize:  1 << 28,
		Daily:    true,
		MaxDays:  31, //我就是喜欢31天，咋滴，不喜欢你就自己改-_-!
		Rotate:   true,
		Level:    logs.LevelTrace,
		//Perm:     "",
	}
}
